1*67e74705SXin Li //===- ClangAttrEmitter.cpp - Generate Clang attribute handling =-*- C++ -*--=//
2*67e74705SXin Li //
3*67e74705SXin Li // The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li // These tablegen backends emit Clang attribute processing code
11*67e74705SXin Li //
12*67e74705SXin Li //===----------------------------------------------------------------------===//
13*67e74705SXin Li
14*67e74705SXin Li #include "llvm/ADT/ArrayRef.h"
15*67e74705SXin Li #include "llvm/ADT/iterator_range.h"
16*67e74705SXin Li #include "llvm/ADT/SmallString.h"
17*67e74705SXin Li #include "llvm/ADT/STLExtras.h"
18*67e74705SXin Li #include "llvm/ADT/StringExtras.h"
19*67e74705SXin Li #include "llvm/ADT/StringRef.h"
20*67e74705SXin Li #include "llvm/ADT/StringSwitch.h"
21*67e74705SXin Li #include "llvm/Support/ErrorHandling.h"
22*67e74705SXin Li #include "llvm/Support/raw_ostream.h"
23*67e74705SXin Li #include "llvm/TableGen/Error.h"
24*67e74705SXin Li #include "llvm/TableGen/Record.h"
25*67e74705SXin Li #include "llvm/TableGen/StringMatcher.h"
26*67e74705SXin Li #include "llvm/TableGen/TableGenBackend.h"
27*67e74705SXin Li #include <algorithm>
28*67e74705SXin Li #include <cassert>
29*67e74705SXin Li #include <cctype>
30*67e74705SXin Li #include <cstddef>
31*67e74705SXin Li #include <cstdint>
32*67e74705SXin Li #include <map>
33*67e74705SXin Li #include <memory>
34*67e74705SXin Li #include <set>
35*67e74705SXin Li #include <sstream>
36*67e74705SXin Li #include <string>
37*67e74705SXin Li #include <utility>
38*67e74705SXin Li #include <vector>
39*67e74705SXin Li
40*67e74705SXin Li using namespace llvm;
41*67e74705SXin Li
42*67e74705SXin Li namespace {
43*67e74705SXin Li
44*67e74705SXin Li class FlattenedSpelling {
45*67e74705SXin Li std::string V, N, NS;
46*67e74705SXin Li bool K;
47*67e74705SXin Li
48*67e74705SXin Li public:
FlattenedSpelling(const std::string & Variety,const std::string & Name,const std::string & Namespace,bool KnownToGCC)49*67e74705SXin Li FlattenedSpelling(const std::string &Variety, const std::string &Name,
50*67e74705SXin Li const std::string &Namespace, bool KnownToGCC) :
51*67e74705SXin Li V(Variety), N(Name), NS(Namespace), K(KnownToGCC) {}
FlattenedSpelling(const Record & Spelling)52*67e74705SXin Li explicit FlattenedSpelling(const Record &Spelling) :
53*67e74705SXin Li V(Spelling.getValueAsString("Variety")),
54*67e74705SXin Li N(Spelling.getValueAsString("Name")) {
55*67e74705SXin Li
56*67e74705SXin Li assert(V != "GCC" && "Given a GCC spelling, which means this hasn't been"
57*67e74705SXin Li "flattened!");
58*67e74705SXin Li if (V == "CXX11" || V == "Pragma")
59*67e74705SXin Li NS = Spelling.getValueAsString("Namespace");
60*67e74705SXin Li bool Unset;
61*67e74705SXin Li K = Spelling.getValueAsBitOrUnset("KnownToGCC", Unset);
62*67e74705SXin Li }
63*67e74705SXin Li
variety() const64*67e74705SXin Li const std::string &variety() const { return V; }
name() const65*67e74705SXin Li const std::string &name() const { return N; }
nameSpace() const66*67e74705SXin Li const std::string &nameSpace() const { return NS; }
knownToGCC() const67*67e74705SXin Li bool knownToGCC() const { return K; }
68*67e74705SXin Li };
69*67e74705SXin Li
70*67e74705SXin Li } // end anonymous namespace
71*67e74705SXin Li
72*67e74705SXin Li static std::vector<FlattenedSpelling>
GetFlattenedSpellings(const Record & Attr)73*67e74705SXin Li GetFlattenedSpellings(const Record &Attr) {
74*67e74705SXin Li std::vector<Record *> Spellings = Attr.getValueAsListOfDefs("Spellings");
75*67e74705SXin Li std::vector<FlattenedSpelling> Ret;
76*67e74705SXin Li
77*67e74705SXin Li for (const auto &Spelling : Spellings) {
78*67e74705SXin Li if (Spelling->getValueAsString("Variety") == "GCC") {
79*67e74705SXin Li // Gin up two new spelling objects to add into the list.
80*67e74705SXin Li Ret.emplace_back("GNU", Spelling->getValueAsString("Name"), "", true);
81*67e74705SXin Li Ret.emplace_back("CXX11", Spelling->getValueAsString("Name"), "gnu",
82*67e74705SXin Li true);
83*67e74705SXin Li } else
84*67e74705SXin Li Ret.push_back(FlattenedSpelling(*Spelling));
85*67e74705SXin Li }
86*67e74705SXin Li
87*67e74705SXin Li return Ret;
88*67e74705SXin Li }
89*67e74705SXin Li
ReadPCHRecord(StringRef type)90*67e74705SXin Li static std::string ReadPCHRecord(StringRef type) {
91*67e74705SXin Li return StringSwitch<std::string>(type)
92*67e74705SXin Li .EndsWith("Decl *", "GetLocalDeclAs<"
93*67e74705SXin Li + std::string(type, 0, type.size()-1) + ">(F, Record[Idx++])")
94*67e74705SXin Li .Case("TypeSourceInfo *", "GetTypeSourceInfo(F, Record, Idx)")
95*67e74705SXin Li .Case("Expr *", "ReadExpr(F)")
96*67e74705SXin Li .Case("IdentifierInfo *", "GetIdentifierInfo(F, Record, Idx)")
97*67e74705SXin Li .Case("StringRef", "ReadString(Record, Idx)")
98*67e74705SXin Li .Default("Record[Idx++]");
99*67e74705SXin Li }
100*67e74705SXin Li
101*67e74705SXin Li // Get a type that is suitable for storing an object of the specified type.
getStorageType(StringRef type)102*67e74705SXin Li static StringRef getStorageType(StringRef type) {
103*67e74705SXin Li return StringSwitch<StringRef>(type)
104*67e74705SXin Li .Case("StringRef", "std::string")
105*67e74705SXin Li .Default(type);
106*67e74705SXin Li }
107*67e74705SXin Li
108*67e74705SXin Li // Assumes that the way to get the value is SA->getname()
WritePCHRecord(StringRef type,StringRef name)109*67e74705SXin Li static std::string WritePCHRecord(StringRef type, StringRef name) {
110*67e74705SXin Li return "Record." + StringSwitch<std::string>(type)
111*67e74705SXin Li .EndsWith("Decl *", "AddDeclRef(" + std::string(name) + ");\n")
112*67e74705SXin Li .Case("TypeSourceInfo *", "AddTypeSourceInfo(" + std::string(name) + ");\n")
113*67e74705SXin Li .Case("Expr *", "AddStmt(" + std::string(name) + ");\n")
114*67e74705SXin Li .Case("IdentifierInfo *", "AddIdentifierRef(" + std::string(name) + ");\n")
115*67e74705SXin Li .Case("StringRef", "AddString(" + std::string(name) + ");\n")
116*67e74705SXin Li .Default("push_back(" + std::string(name) + ");\n");
117*67e74705SXin Li }
118*67e74705SXin Li
119*67e74705SXin Li // Normalize attribute name by removing leading and trailing
120*67e74705SXin Li // underscores. For example, __foo, foo__, __foo__ would
121*67e74705SXin Li // become foo.
NormalizeAttrName(StringRef AttrName)122*67e74705SXin Li static StringRef NormalizeAttrName(StringRef AttrName) {
123*67e74705SXin Li if (AttrName.startswith("__"))
124*67e74705SXin Li AttrName = AttrName.substr(2, AttrName.size());
125*67e74705SXin Li
126*67e74705SXin Li if (AttrName.endswith("__"))
127*67e74705SXin Li AttrName = AttrName.substr(0, AttrName.size() - 2);
128*67e74705SXin Li
129*67e74705SXin Li return AttrName;
130*67e74705SXin Li }
131*67e74705SXin Li
132*67e74705SXin Li // Normalize the name by removing any and all leading and trailing underscores.
133*67e74705SXin Li // This is different from NormalizeAttrName in that it also handles names like
134*67e74705SXin Li // _pascal and __pascal.
NormalizeNameForSpellingComparison(StringRef Name)135*67e74705SXin Li static StringRef NormalizeNameForSpellingComparison(StringRef Name) {
136*67e74705SXin Li return Name.trim("_");
137*67e74705SXin Li }
138*67e74705SXin Li
139*67e74705SXin Li // Normalize attribute spelling only if the spelling has both leading
140*67e74705SXin Li // and trailing underscores. For example, __ms_struct__ will be
141*67e74705SXin Li // normalized to "ms_struct"; __cdecl will remain intact.
NormalizeAttrSpelling(StringRef AttrSpelling)142*67e74705SXin Li static StringRef NormalizeAttrSpelling(StringRef AttrSpelling) {
143*67e74705SXin Li if (AttrSpelling.startswith("__") && AttrSpelling.endswith("__")) {
144*67e74705SXin Li AttrSpelling = AttrSpelling.substr(2, AttrSpelling.size() - 4);
145*67e74705SXin Li }
146*67e74705SXin Li
147*67e74705SXin Li return AttrSpelling;
148*67e74705SXin Li }
149*67e74705SXin Li
150*67e74705SXin Li typedef std::vector<std::pair<std::string, const Record *>> ParsedAttrMap;
151*67e74705SXin Li
getParsedAttrList(const RecordKeeper & Records,ParsedAttrMap * Dupes=nullptr)152*67e74705SXin Li static ParsedAttrMap getParsedAttrList(const RecordKeeper &Records,
153*67e74705SXin Li ParsedAttrMap *Dupes = nullptr) {
154*67e74705SXin Li std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
155*67e74705SXin Li std::set<std::string> Seen;
156*67e74705SXin Li ParsedAttrMap R;
157*67e74705SXin Li for (const auto *Attr : Attrs) {
158*67e74705SXin Li if (Attr->getValueAsBit("SemaHandler")) {
159*67e74705SXin Li std::string AN;
160*67e74705SXin Li if (Attr->isSubClassOf("TargetSpecificAttr") &&
161*67e74705SXin Li !Attr->isValueUnset("ParseKind")) {
162*67e74705SXin Li AN = Attr->getValueAsString("ParseKind");
163*67e74705SXin Li
164*67e74705SXin Li // If this attribute has already been handled, it does not need to be
165*67e74705SXin Li // handled again.
166*67e74705SXin Li if (Seen.find(AN) != Seen.end()) {
167*67e74705SXin Li if (Dupes)
168*67e74705SXin Li Dupes->push_back(std::make_pair(AN, Attr));
169*67e74705SXin Li continue;
170*67e74705SXin Li }
171*67e74705SXin Li Seen.insert(AN);
172*67e74705SXin Li } else
173*67e74705SXin Li AN = NormalizeAttrName(Attr->getName()).str();
174*67e74705SXin Li
175*67e74705SXin Li R.push_back(std::make_pair(AN, Attr));
176*67e74705SXin Li }
177*67e74705SXin Li }
178*67e74705SXin Li return R;
179*67e74705SXin Li }
180*67e74705SXin Li
181*67e74705SXin Li namespace {
182*67e74705SXin Li
183*67e74705SXin Li class Argument {
184*67e74705SXin Li std::string lowerName, upperName;
185*67e74705SXin Li StringRef attrName;
186*67e74705SXin Li bool isOpt;
187*67e74705SXin Li bool Fake;
188*67e74705SXin Li
189*67e74705SXin Li public:
Argument(const Record & Arg,StringRef Attr)190*67e74705SXin Li Argument(const Record &Arg, StringRef Attr)
191*67e74705SXin Li : lowerName(Arg.getValueAsString("Name")), upperName(lowerName),
192*67e74705SXin Li attrName(Attr), isOpt(false), Fake(false) {
193*67e74705SXin Li if (!lowerName.empty()) {
194*67e74705SXin Li lowerName[0] = std::tolower(lowerName[0]);
195*67e74705SXin Li upperName[0] = std::toupper(upperName[0]);
196*67e74705SXin Li }
197*67e74705SXin Li // Work around MinGW's macro definition of 'interface' to 'struct'. We
198*67e74705SXin Li // have an attribute argument called 'Interface', so only the lower case
199*67e74705SXin Li // name conflicts with the macro definition.
200*67e74705SXin Li if (lowerName == "interface")
201*67e74705SXin Li lowerName = "interface_";
202*67e74705SXin Li }
203*67e74705SXin Li virtual ~Argument() = default;
204*67e74705SXin Li
getLowerName() const205*67e74705SXin Li StringRef getLowerName() const { return lowerName; }
getUpperName() const206*67e74705SXin Li StringRef getUpperName() const { return upperName; }
getAttrName() const207*67e74705SXin Li StringRef getAttrName() const { return attrName; }
208*67e74705SXin Li
isOptional() const209*67e74705SXin Li bool isOptional() const { return isOpt; }
setOptional(bool set)210*67e74705SXin Li void setOptional(bool set) { isOpt = set; }
211*67e74705SXin Li
isFake() const212*67e74705SXin Li bool isFake() const { return Fake; }
setFake(bool fake)213*67e74705SXin Li void setFake(bool fake) { Fake = fake; }
214*67e74705SXin Li
215*67e74705SXin Li // These functions print the argument contents formatted in different ways.
216*67e74705SXin Li virtual void writeAccessors(raw_ostream &OS) const = 0;
writeAccessorDefinitions(raw_ostream & OS) const217*67e74705SXin Li virtual void writeAccessorDefinitions(raw_ostream &OS) const {}
writeASTVisitorTraversal(raw_ostream & OS) const218*67e74705SXin Li virtual void writeASTVisitorTraversal(raw_ostream &OS) const {}
219*67e74705SXin Li virtual void writeCloneArgs(raw_ostream &OS) const = 0;
220*67e74705SXin Li virtual void writeTemplateInstantiationArgs(raw_ostream &OS) const = 0;
writeTemplateInstantiation(raw_ostream & OS) const221*67e74705SXin Li virtual void writeTemplateInstantiation(raw_ostream &OS) const {}
writeCtorBody(raw_ostream & OS) const222*67e74705SXin Li virtual void writeCtorBody(raw_ostream &OS) const {}
223*67e74705SXin Li virtual void writeCtorInitializers(raw_ostream &OS) const = 0;
224*67e74705SXin Li virtual void writeCtorDefaultInitializers(raw_ostream &OS) const = 0;
225*67e74705SXin Li virtual void writeCtorParameters(raw_ostream &OS) const = 0;
226*67e74705SXin Li virtual void writeDeclarations(raw_ostream &OS) const = 0;
227*67e74705SXin Li virtual void writePCHReadArgs(raw_ostream &OS) const = 0;
228*67e74705SXin Li virtual void writePCHReadDecls(raw_ostream &OS) const = 0;
229*67e74705SXin Li virtual void writePCHWrite(raw_ostream &OS) const = 0;
230*67e74705SXin Li virtual void writeValue(raw_ostream &OS) const = 0;
231*67e74705SXin Li virtual void writeDump(raw_ostream &OS) const = 0;
writeDumpChildren(raw_ostream & OS) const232*67e74705SXin Li virtual void writeDumpChildren(raw_ostream &OS) const {}
writeHasChildren(raw_ostream & OS) const233*67e74705SXin Li virtual void writeHasChildren(raw_ostream &OS) const { OS << "false"; }
234*67e74705SXin Li
isEnumArg() const235*67e74705SXin Li virtual bool isEnumArg() const { return false; }
isVariadicEnumArg() const236*67e74705SXin Li virtual bool isVariadicEnumArg() const { return false; }
isVariadic() const237*67e74705SXin Li virtual bool isVariadic() const { return false; }
238*67e74705SXin Li
writeImplicitCtorArgs(raw_ostream & OS) const239*67e74705SXin Li virtual void writeImplicitCtorArgs(raw_ostream &OS) const {
240*67e74705SXin Li OS << getUpperName();
241*67e74705SXin Li }
242*67e74705SXin Li };
243*67e74705SXin Li
244*67e74705SXin Li class SimpleArgument : public Argument {
245*67e74705SXin Li std::string type;
246*67e74705SXin Li
247*67e74705SXin Li public:
SimpleArgument(const Record & Arg,StringRef Attr,std::string T)248*67e74705SXin Li SimpleArgument(const Record &Arg, StringRef Attr, std::string T)
249*67e74705SXin Li : Argument(Arg, Attr), type(std::move(T)) {}
250*67e74705SXin Li
getType() const251*67e74705SXin Li std::string getType() const { return type; }
252*67e74705SXin Li
writeAccessors(raw_ostream & OS) const253*67e74705SXin Li void writeAccessors(raw_ostream &OS) const override {
254*67e74705SXin Li OS << " " << type << " get" << getUpperName() << "() const {\n";
255*67e74705SXin Li OS << " return " << getLowerName() << ";\n";
256*67e74705SXin Li OS << " }";
257*67e74705SXin Li }
258*67e74705SXin Li
writeCloneArgs(raw_ostream & OS) const259*67e74705SXin Li void writeCloneArgs(raw_ostream &OS) const override {
260*67e74705SXin Li OS << getLowerName();
261*67e74705SXin Li }
262*67e74705SXin Li
writeTemplateInstantiationArgs(raw_ostream & OS) const263*67e74705SXin Li void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
264*67e74705SXin Li OS << "A->get" << getUpperName() << "()";
265*67e74705SXin Li }
266*67e74705SXin Li
writeCtorInitializers(raw_ostream & OS) const267*67e74705SXin Li void writeCtorInitializers(raw_ostream &OS) const override {
268*67e74705SXin Li OS << getLowerName() << "(" << getUpperName() << ")";
269*67e74705SXin Li }
270*67e74705SXin Li
writeCtorDefaultInitializers(raw_ostream & OS) const271*67e74705SXin Li void writeCtorDefaultInitializers(raw_ostream &OS) const override {
272*67e74705SXin Li OS << getLowerName() << "()";
273*67e74705SXin Li }
274*67e74705SXin Li
writeCtorParameters(raw_ostream & OS) const275*67e74705SXin Li void writeCtorParameters(raw_ostream &OS) const override {
276*67e74705SXin Li OS << type << " " << getUpperName();
277*67e74705SXin Li }
278*67e74705SXin Li
writeDeclarations(raw_ostream & OS) const279*67e74705SXin Li void writeDeclarations(raw_ostream &OS) const override {
280*67e74705SXin Li OS << type << " " << getLowerName() << ";";
281*67e74705SXin Li }
282*67e74705SXin Li
writePCHReadDecls(raw_ostream & OS) const283*67e74705SXin Li void writePCHReadDecls(raw_ostream &OS) const override {
284*67e74705SXin Li std::string read = ReadPCHRecord(type);
285*67e74705SXin Li OS << " " << type << " " << getLowerName() << " = " << read << ";\n";
286*67e74705SXin Li }
287*67e74705SXin Li
writePCHReadArgs(raw_ostream & OS) const288*67e74705SXin Li void writePCHReadArgs(raw_ostream &OS) const override {
289*67e74705SXin Li OS << getLowerName();
290*67e74705SXin Li }
291*67e74705SXin Li
writePCHWrite(raw_ostream & OS) const292*67e74705SXin Li void writePCHWrite(raw_ostream &OS) const override {
293*67e74705SXin Li OS << " " << WritePCHRecord(type, "SA->get" +
294*67e74705SXin Li std::string(getUpperName()) + "()");
295*67e74705SXin Li }
296*67e74705SXin Li
writeValue(raw_ostream & OS) const297*67e74705SXin Li void writeValue(raw_ostream &OS) const override {
298*67e74705SXin Li if (type == "FunctionDecl *") {
299*67e74705SXin Li OS << "\" << get" << getUpperName()
300*67e74705SXin Li << "()->getNameInfo().getAsString() << \"";
301*67e74705SXin Li } else if (type == "IdentifierInfo *") {
302*67e74705SXin Li OS << "\" << get" << getUpperName() << "()->getName() << \"";
303*67e74705SXin Li } else if (type == "TypeSourceInfo *") {
304*67e74705SXin Li OS << "\" << get" << getUpperName() << "().getAsString() << \"";
305*67e74705SXin Li } else {
306*67e74705SXin Li OS << "\" << get" << getUpperName() << "() << \"";
307*67e74705SXin Li }
308*67e74705SXin Li }
309*67e74705SXin Li
writeDump(raw_ostream & OS) const310*67e74705SXin Li void writeDump(raw_ostream &OS) const override {
311*67e74705SXin Li if (type == "FunctionDecl *") {
312*67e74705SXin Li OS << " OS << \" \";\n";
313*67e74705SXin Li OS << " dumpBareDeclRef(SA->get" << getUpperName() << "());\n";
314*67e74705SXin Li } else if (type == "IdentifierInfo *") {
315*67e74705SXin Li if (isOptional())
316*67e74705SXin Li OS << " if (SA->get" << getUpperName() << "())\n ";
317*67e74705SXin Li OS << " OS << \" \" << SA->get" << getUpperName()
318*67e74705SXin Li << "()->getName();\n";
319*67e74705SXin Li } else if (type == "TypeSourceInfo *") {
320*67e74705SXin Li OS << " OS << \" \" << SA->get" << getUpperName()
321*67e74705SXin Li << "().getAsString();\n";
322*67e74705SXin Li } else if (type == "bool") {
323*67e74705SXin Li OS << " if (SA->get" << getUpperName() << "()) OS << \" "
324*67e74705SXin Li << getUpperName() << "\";\n";
325*67e74705SXin Li } else if (type == "int" || type == "unsigned") {
326*67e74705SXin Li OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
327*67e74705SXin Li } else {
328*67e74705SXin Li llvm_unreachable("Unknown SimpleArgument type!");
329*67e74705SXin Li }
330*67e74705SXin Li }
331*67e74705SXin Li };
332*67e74705SXin Li
333*67e74705SXin Li class DefaultSimpleArgument : public SimpleArgument {
334*67e74705SXin Li int64_t Default;
335*67e74705SXin Li
336*67e74705SXin Li public:
DefaultSimpleArgument(const Record & Arg,StringRef Attr,std::string T,int64_t Default)337*67e74705SXin Li DefaultSimpleArgument(const Record &Arg, StringRef Attr,
338*67e74705SXin Li std::string T, int64_t Default)
339*67e74705SXin Li : SimpleArgument(Arg, Attr, T), Default(Default) {}
340*67e74705SXin Li
writeAccessors(raw_ostream & OS) const341*67e74705SXin Li void writeAccessors(raw_ostream &OS) const override {
342*67e74705SXin Li SimpleArgument::writeAccessors(OS);
343*67e74705SXin Li
344*67e74705SXin Li OS << "\n\n static const " << getType() << " Default" << getUpperName()
345*67e74705SXin Li << " = ";
346*67e74705SXin Li if (getType() == "bool")
347*67e74705SXin Li OS << (Default != 0 ? "true" : "false");
348*67e74705SXin Li else
349*67e74705SXin Li OS << Default;
350*67e74705SXin Li OS << ";";
351*67e74705SXin Li }
352*67e74705SXin Li };
353*67e74705SXin Li
354*67e74705SXin Li class StringArgument : public Argument {
355*67e74705SXin Li public:
StringArgument(const Record & Arg,StringRef Attr)356*67e74705SXin Li StringArgument(const Record &Arg, StringRef Attr)
357*67e74705SXin Li : Argument(Arg, Attr)
358*67e74705SXin Li {}
359*67e74705SXin Li
writeAccessors(raw_ostream & OS) const360*67e74705SXin Li void writeAccessors(raw_ostream &OS) const override {
361*67e74705SXin Li OS << " llvm::StringRef get" << getUpperName() << "() const {\n";
362*67e74705SXin Li OS << " return llvm::StringRef(" << getLowerName() << ", "
363*67e74705SXin Li << getLowerName() << "Length);\n";
364*67e74705SXin Li OS << " }\n";
365*67e74705SXin Li OS << " unsigned get" << getUpperName() << "Length() const {\n";
366*67e74705SXin Li OS << " return " << getLowerName() << "Length;\n";
367*67e74705SXin Li OS << " }\n";
368*67e74705SXin Li OS << " void set" << getUpperName()
369*67e74705SXin Li << "(ASTContext &C, llvm::StringRef S) {\n";
370*67e74705SXin Li OS << " " << getLowerName() << "Length = S.size();\n";
371*67e74705SXin Li OS << " this->" << getLowerName() << " = new (C, 1) char ["
372*67e74705SXin Li << getLowerName() << "Length];\n";
373*67e74705SXin Li OS << " if (!S.empty())\n";
374*67e74705SXin Li OS << " std::memcpy(this->" << getLowerName() << ", S.data(), "
375*67e74705SXin Li << getLowerName() << "Length);\n";
376*67e74705SXin Li OS << " }";
377*67e74705SXin Li }
378*67e74705SXin Li
writeCloneArgs(raw_ostream & OS) const379*67e74705SXin Li void writeCloneArgs(raw_ostream &OS) const override {
380*67e74705SXin Li OS << "get" << getUpperName() << "()";
381*67e74705SXin Li }
382*67e74705SXin Li
writeTemplateInstantiationArgs(raw_ostream & OS) const383*67e74705SXin Li void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
384*67e74705SXin Li OS << "A->get" << getUpperName() << "()";
385*67e74705SXin Li }
386*67e74705SXin Li
writeCtorBody(raw_ostream & OS) const387*67e74705SXin Li void writeCtorBody(raw_ostream &OS) const override {
388*67e74705SXin Li OS << " if (!" << getUpperName() << ".empty())\n";
389*67e74705SXin Li OS << " std::memcpy(" << getLowerName() << ", " << getUpperName()
390*67e74705SXin Li << ".data(), " << getLowerName() << "Length);\n";
391*67e74705SXin Li }
392*67e74705SXin Li
writeCtorInitializers(raw_ostream & OS) const393*67e74705SXin Li void writeCtorInitializers(raw_ostream &OS) const override {
394*67e74705SXin Li OS << getLowerName() << "Length(" << getUpperName() << ".size()),"
395*67e74705SXin Li << getLowerName() << "(new (Ctx, 1) char[" << getLowerName()
396*67e74705SXin Li << "Length])";
397*67e74705SXin Li }
398*67e74705SXin Li
writeCtorDefaultInitializers(raw_ostream & OS) const399*67e74705SXin Li void writeCtorDefaultInitializers(raw_ostream &OS) const override {
400*67e74705SXin Li OS << getLowerName() << "Length(0)," << getLowerName() << "(nullptr)";
401*67e74705SXin Li }
402*67e74705SXin Li
writeCtorParameters(raw_ostream & OS) const403*67e74705SXin Li void writeCtorParameters(raw_ostream &OS) const override {
404*67e74705SXin Li OS << "llvm::StringRef " << getUpperName();
405*67e74705SXin Li }
406*67e74705SXin Li
writeDeclarations(raw_ostream & OS) const407*67e74705SXin Li void writeDeclarations(raw_ostream &OS) const override {
408*67e74705SXin Li OS << "unsigned " << getLowerName() << "Length;\n";
409*67e74705SXin Li OS << "char *" << getLowerName() << ";";
410*67e74705SXin Li }
411*67e74705SXin Li
writePCHReadDecls(raw_ostream & OS) const412*67e74705SXin Li void writePCHReadDecls(raw_ostream &OS) const override {
413*67e74705SXin Li OS << " std::string " << getLowerName()
414*67e74705SXin Li << "= ReadString(Record, Idx);\n";
415*67e74705SXin Li }
416*67e74705SXin Li
writePCHReadArgs(raw_ostream & OS) const417*67e74705SXin Li void writePCHReadArgs(raw_ostream &OS) const override {
418*67e74705SXin Li OS << getLowerName();
419*67e74705SXin Li }
420*67e74705SXin Li
writePCHWrite(raw_ostream & OS) const421*67e74705SXin Li void writePCHWrite(raw_ostream &OS) const override {
422*67e74705SXin Li OS << " Record.AddString(SA->get" << getUpperName() << "());\n";
423*67e74705SXin Li }
424*67e74705SXin Li
writeValue(raw_ostream & OS) const425*67e74705SXin Li void writeValue(raw_ostream &OS) const override {
426*67e74705SXin Li OS << "\\\"\" << get" << getUpperName() << "() << \"\\\"";
427*67e74705SXin Li }
428*67e74705SXin Li
writeDump(raw_ostream & OS) const429*67e74705SXin Li void writeDump(raw_ostream &OS) const override {
430*67e74705SXin Li OS << " OS << \" \\\"\" << SA->get" << getUpperName()
431*67e74705SXin Li << "() << \"\\\"\";\n";
432*67e74705SXin Li }
433*67e74705SXin Li };
434*67e74705SXin Li
435*67e74705SXin Li class AlignedArgument : public Argument {
436*67e74705SXin Li public:
AlignedArgument(const Record & Arg,StringRef Attr)437*67e74705SXin Li AlignedArgument(const Record &Arg, StringRef Attr)
438*67e74705SXin Li : Argument(Arg, Attr)
439*67e74705SXin Li {}
440*67e74705SXin Li
writeAccessors(raw_ostream & OS) const441*67e74705SXin Li void writeAccessors(raw_ostream &OS) const override {
442*67e74705SXin Li OS << " bool is" << getUpperName() << "Dependent() const;\n";
443*67e74705SXin Li
444*67e74705SXin Li OS << " unsigned get" << getUpperName() << "(ASTContext &Ctx) const;\n";
445*67e74705SXin Li
446*67e74705SXin Li OS << " bool is" << getUpperName() << "Expr() const {\n";
447*67e74705SXin Li OS << " return is" << getLowerName() << "Expr;\n";
448*67e74705SXin Li OS << " }\n";
449*67e74705SXin Li
450*67e74705SXin Li OS << " Expr *get" << getUpperName() << "Expr() const {\n";
451*67e74705SXin Li OS << " assert(is" << getLowerName() << "Expr);\n";
452*67e74705SXin Li OS << " return " << getLowerName() << "Expr;\n";
453*67e74705SXin Li OS << " }\n";
454*67e74705SXin Li
455*67e74705SXin Li OS << " TypeSourceInfo *get" << getUpperName() << "Type() const {\n";
456*67e74705SXin Li OS << " assert(!is" << getLowerName() << "Expr);\n";
457*67e74705SXin Li OS << " return " << getLowerName() << "Type;\n";
458*67e74705SXin Li OS << " }";
459*67e74705SXin Li }
460*67e74705SXin Li
writeAccessorDefinitions(raw_ostream & OS) const461*67e74705SXin Li void writeAccessorDefinitions(raw_ostream &OS) const override {
462*67e74705SXin Li OS << "bool " << getAttrName() << "Attr::is" << getUpperName()
463*67e74705SXin Li << "Dependent() const {\n";
464*67e74705SXin Li OS << " if (is" << getLowerName() << "Expr)\n";
465*67e74705SXin Li OS << " return " << getLowerName() << "Expr && (" << getLowerName()
466*67e74705SXin Li << "Expr->isValueDependent() || " << getLowerName()
467*67e74705SXin Li << "Expr->isTypeDependent());\n";
468*67e74705SXin Li OS << " else\n";
469*67e74705SXin Li OS << " return " << getLowerName()
470*67e74705SXin Li << "Type->getType()->isDependentType();\n";
471*67e74705SXin Li OS << "}\n";
472*67e74705SXin Li
473*67e74705SXin Li // FIXME: Do not do the calculation here
474*67e74705SXin Li // FIXME: Handle types correctly
475*67e74705SXin Li // A null pointer means maximum alignment
476*67e74705SXin Li OS << "unsigned " << getAttrName() << "Attr::get" << getUpperName()
477*67e74705SXin Li << "(ASTContext &Ctx) const {\n";
478*67e74705SXin Li OS << " assert(!is" << getUpperName() << "Dependent());\n";
479*67e74705SXin Li OS << " if (is" << getLowerName() << "Expr)\n";
480*67e74705SXin Li OS << " return " << getLowerName() << "Expr ? " << getLowerName()
481*67e74705SXin Li << "Expr->EvaluateKnownConstInt(Ctx).getZExtValue()"
482*67e74705SXin Li << " * Ctx.getCharWidth() : "
483*67e74705SXin Li << "Ctx.getTargetDefaultAlignForAttributeAligned();\n";
484*67e74705SXin Li OS << " else\n";
485*67e74705SXin Li OS << " return 0; // FIXME\n";
486*67e74705SXin Li OS << "}\n";
487*67e74705SXin Li }
488*67e74705SXin Li
writeCloneArgs(raw_ostream & OS) const489*67e74705SXin Li void writeCloneArgs(raw_ostream &OS) const override {
490*67e74705SXin Li OS << "is" << getLowerName() << "Expr, is" << getLowerName()
491*67e74705SXin Li << "Expr ? static_cast<void*>(" << getLowerName()
492*67e74705SXin Li << "Expr) : " << getLowerName()
493*67e74705SXin Li << "Type";
494*67e74705SXin Li }
495*67e74705SXin Li
writeTemplateInstantiationArgs(raw_ostream & OS) const496*67e74705SXin Li void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
497*67e74705SXin Li // FIXME: move the definition in Sema::InstantiateAttrs to here.
498*67e74705SXin Li // In the meantime, aligned attributes are cloned.
499*67e74705SXin Li }
500*67e74705SXin Li
writeCtorBody(raw_ostream & OS) const501*67e74705SXin Li void writeCtorBody(raw_ostream &OS) const override {
502*67e74705SXin Li OS << " if (is" << getLowerName() << "Expr)\n";
503*67e74705SXin Li OS << " " << getLowerName() << "Expr = reinterpret_cast<Expr *>("
504*67e74705SXin Li << getUpperName() << ");\n";
505*67e74705SXin Li OS << " else\n";
506*67e74705SXin Li OS << " " << getLowerName()
507*67e74705SXin Li << "Type = reinterpret_cast<TypeSourceInfo *>(" << getUpperName()
508*67e74705SXin Li << ");\n";
509*67e74705SXin Li }
510*67e74705SXin Li
writeCtorInitializers(raw_ostream & OS) const511*67e74705SXin Li void writeCtorInitializers(raw_ostream &OS) const override {
512*67e74705SXin Li OS << "is" << getLowerName() << "Expr(Is" << getUpperName() << "Expr)";
513*67e74705SXin Li }
514*67e74705SXin Li
writeCtorDefaultInitializers(raw_ostream & OS) const515*67e74705SXin Li void writeCtorDefaultInitializers(raw_ostream &OS) const override {
516*67e74705SXin Li OS << "is" << getLowerName() << "Expr(false)";
517*67e74705SXin Li }
518*67e74705SXin Li
writeCtorParameters(raw_ostream & OS) const519*67e74705SXin Li void writeCtorParameters(raw_ostream &OS) const override {
520*67e74705SXin Li OS << "bool Is" << getUpperName() << "Expr, void *" << getUpperName();
521*67e74705SXin Li }
522*67e74705SXin Li
writeImplicitCtorArgs(raw_ostream & OS) const523*67e74705SXin Li void writeImplicitCtorArgs(raw_ostream &OS) const override {
524*67e74705SXin Li OS << "Is" << getUpperName() << "Expr, " << getUpperName();
525*67e74705SXin Li }
526*67e74705SXin Li
writeDeclarations(raw_ostream & OS) const527*67e74705SXin Li void writeDeclarations(raw_ostream &OS) const override {
528*67e74705SXin Li OS << "bool is" << getLowerName() << "Expr;\n";
529*67e74705SXin Li OS << "union {\n";
530*67e74705SXin Li OS << "Expr *" << getLowerName() << "Expr;\n";
531*67e74705SXin Li OS << "TypeSourceInfo *" << getLowerName() << "Type;\n";
532*67e74705SXin Li OS << "};";
533*67e74705SXin Li }
534*67e74705SXin Li
writePCHReadArgs(raw_ostream & OS) const535*67e74705SXin Li void writePCHReadArgs(raw_ostream &OS) const override {
536*67e74705SXin Li OS << "is" << getLowerName() << "Expr, " << getLowerName() << "Ptr";
537*67e74705SXin Li }
538*67e74705SXin Li
writePCHReadDecls(raw_ostream & OS) const539*67e74705SXin Li void writePCHReadDecls(raw_ostream &OS) const override {
540*67e74705SXin Li OS << " bool is" << getLowerName() << "Expr = Record[Idx++];\n";
541*67e74705SXin Li OS << " void *" << getLowerName() << "Ptr;\n";
542*67e74705SXin Li OS << " if (is" << getLowerName() << "Expr)\n";
543*67e74705SXin Li OS << " " << getLowerName() << "Ptr = ReadExpr(F);\n";
544*67e74705SXin Li OS << " else\n";
545*67e74705SXin Li OS << " " << getLowerName()
546*67e74705SXin Li << "Ptr = GetTypeSourceInfo(F, Record, Idx);\n";
547*67e74705SXin Li }
548*67e74705SXin Li
writePCHWrite(raw_ostream & OS) const549*67e74705SXin Li void writePCHWrite(raw_ostream &OS) const override {
550*67e74705SXin Li OS << " Record.push_back(SA->is" << getUpperName() << "Expr());\n";
551*67e74705SXin Li OS << " if (SA->is" << getUpperName() << "Expr())\n";
552*67e74705SXin Li OS << " Record.AddStmt(SA->get" << getUpperName() << "Expr());\n";
553*67e74705SXin Li OS << " else\n";
554*67e74705SXin Li OS << " Record.AddTypeSourceInfo(SA->get" << getUpperName()
555*67e74705SXin Li << "Type());\n";
556*67e74705SXin Li }
557*67e74705SXin Li
writeValue(raw_ostream & OS) const558*67e74705SXin Li void writeValue(raw_ostream &OS) const override {
559*67e74705SXin Li OS << "\";\n";
560*67e74705SXin Li // The aligned attribute argument expression is optional.
561*67e74705SXin Li OS << " if (is" << getLowerName() << "Expr && "
562*67e74705SXin Li << getLowerName() << "Expr)\n";
563*67e74705SXin Li OS << " " << getLowerName() << "Expr->printPretty(OS, nullptr, Policy);\n";
564*67e74705SXin Li OS << " OS << \"";
565*67e74705SXin Li }
566*67e74705SXin Li
writeDump(raw_ostream & OS) const567*67e74705SXin Li void writeDump(raw_ostream &OS) const override {}
568*67e74705SXin Li
writeDumpChildren(raw_ostream & OS) const569*67e74705SXin Li void writeDumpChildren(raw_ostream &OS) const override {
570*67e74705SXin Li OS << " if (SA->is" << getUpperName() << "Expr())\n";
571*67e74705SXin Li OS << " dumpStmt(SA->get" << getUpperName() << "Expr());\n";
572*67e74705SXin Li OS << " else\n";
573*67e74705SXin Li OS << " dumpType(SA->get" << getUpperName()
574*67e74705SXin Li << "Type()->getType());\n";
575*67e74705SXin Li }
576*67e74705SXin Li
writeHasChildren(raw_ostream & OS) const577*67e74705SXin Li void writeHasChildren(raw_ostream &OS) const override {
578*67e74705SXin Li OS << "SA->is" << getUpperName() << "Expr()";
579*67e74705SXin Li }
580*67e74705SXin Li };
581*67e74705SXin Li
582*67e74705SXin Li class VariadicArgument : public Argument {
583*67e74705SXin Li std::string Type, ArgName, ArgSizeName, RangeName;
584*67e74705SXin Li
585*67e74705SXin Li protected:
586*67e74705SXin Li // Assumed to receive a parameter: raw_ostream OS.
writeValueImpl(raw_ostream & OS) const587*67e74705SXin Li virtual void writeValueImpl(raw_ostream &OS) const {
588*67e74705SXin Li OS << " OS << Val;\n";
589*67e74705SXin Li }
590*67e74705SXin Li
591*67e74705SXin Li public:
VariadicArgument(const Record & Arg,StringRef Attr,std::string T)592*67e74705SXin Li VariadicArgument(const Record &Arg, StringRef Attr, std::string T)
593*67e74705SXin Li : Argument(Arg, Attr), Type(std::move(T)),
594*67e74705SXin Li ArgName(getLowerName().str() + "_"), ArgSizeName(ArgName + "Size"),
595*67e74705SXin Li RangeName(getLowerName()) {}
596*67e74705SXin Li
getType() const597*67e74705SXin Li const std::string &getType() const { return Type; }
getArgName() const598*67e74705SXin Li const std::string &getArgName() const { return ArgName; }
getArgSizeName() const599*67e74705SXin Li const std::string &getArgSizeName() const { return ArgSizeName; }
isVariadic() const600*67e74705SXin Li bool isVariadic() const override { return true; }
601*67e74705SXin Li
writeAccessors(raw_ostream & OS) const602*67e74705SXin Li void writeAccessors(raw_ostream &OS) const override {
603*67e74705SXin Li std::string IteratorType = getLowerName().str() + "_iterator";
604*67e74705SXin Li std::string BeginFn = getLowerName().str() + "_begin()";
605*67e74705SXin Li std::string EndFn = getLowerName().str() + "_end()";
606*67e74705SXin Li
607*67e74705SXin Li OS << " typedef " << Type << "* " << IteratorType << ";\n";
608*67e74705SXin Li OS << " " << IteratorType << " " << BeginFn << " const {"
609*67e74705SXin Li << " return " << ArgName << "; }\n";
610*67e74705SXin Li OS << " " << IteratorType << " " << EndFn << " const {"
611*67e74705SXin Li << " return " << ArgName << " + " << ArgSizeName << "; }\n";
612*67e74705SXin Li OS << " unsigned " << getLowerName() << "_size() const {"
613*67e74705SXin Li << " return " << ArgSizeName << "; }\n";
614*67e74705SXin Li OS << " llvm::iterator_range<" << IteratorType << "> " << RangeName
615*67e74705SXin Li << "() const { return llvm::make_range(" << BeginFn << ", " << EndFn
616*67e74705SXin Li << "); }\n";
617*67e74705SXin Li }
618*67e74705SXin Li
writeCloneArgs(raw_ostream & OS) const619*67e74705SXin Li void writeCloneArgs(raw_ostream &OS) const override {
620*67e74705SXin Li OS << ArgName << ", " << ArgSizeName;
621*67e74705SXin Li }
622*67e74705SXin Li
writeTemplateInstantiationArgs(raw_ostream & OS) const623*67e74705SXin Li void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
624*67e74705SXin Li // This isn't elegant, but we have to go through public methods...
625*67e74705SXin Li OS << "A->" << getLowerName() << "_begin(), "
626*67e74705SXin Li << "A->" << getLowerName() << "_size()";
627*67e74705SXin Li }
628*67e74705SXin Li
writeCtorBody(raw_ostream & OS) const629*67e74705SXin Li void writeCtorBody(raw_ostream &OS) const override {
630*67e74705SXin Li OS << " std::copy(" << getUpperName() << ", " << getUpperName()
631*67e74705SXin Li << " + " << ArgSizeName << ", " << ArgName << ");\n";
632*67e74705SXin Li }
633*67e74705SXin Li
writeCtorInitializers(raw_ostream & OS) const634*67e74705SXin Li void writeCtorInitializers(raw_ostream &OS) const override {
635*67e74705SXin Li OS << ArgSizeName << "(" << getUpperName() << "Size), "
636*67e74705SXin Li << ArgName << "(new (Ctx, 16) " << getType() << "["
637*67e74705SXin Li << ArgSizeName << "])";
638*67e74705SXin Li }
639*67e74705SXin Li
writeCtorDefaultInitializers(raw_ostream & OS) const640*67e74705SXin Li void writeCtorDefaultInitializers(raw_ostream &OS) const override {
641*67e74705SXin Li OS << ArgSizeName << "(0), " << ArgName << "(nullptr)";
642*67e74705SXin Li }
643*67e74705SXin Li
writeCtorParameters(raw_ostream & OS) const644*67e74705SXin Li void writeCtorParameters(raw_ostream &OS) const override {
645*67e74705SXin Li OS << getType() << " *" << getUpperName() << ", unsigned "
646*67e74705SXin Li << getUpperName() << "Size";
647*67e74705SXin Li }
648*67e74705SXin Li
writeImplicitCtorArgs(raw_ostream & OS) const649*67e74705SXin Li void writeImplicitCtorArgs(raw_ostream &OS) const override {
650*67e74705SXin Li OS << getUpperName() << ", " << getUpperName() << "Size";
651*67e74705SXin Li }
652*67e74705SXin Li
writeDeclarations(raw_ostream & OS) const653*67e74705SXin Li void writeDeclarations(raw_ostream &OS) const override {
654*67e74705SXin Li OS << " unsigned " << ArgSizeName << ";\n";
655*67e74705SXin Li OS << " " << getType() << " *" << ArgName << ";";
656*67e74705SXin Li }
657*67e74705SXin Li
writePCHReadDecls(raw_ostream & OS) const658*67e74705SXin Li void writePCHReadDecls(raw_ostream &OS) const override {
659*67e74705SXin Li OS << " unsigned " << getLowerName() << "Size = Record[Idx++];\n";
660*67e74705SXin Li OS << " SmallVector<" << getType() << ", 4> "
661*67e74705SXin Li << getLowerName() << ";\n";
662*67e74705SXin Li OS << " " << getLowerName() << ".reserve(" << getLowerName()
663*67e74705SXin Li << "Size);\n";
664*67e74705SXin Li
665*67e74705SXin Li // If we can't store the values in the current type (if it's something
666*67e74705SXin Li // like StringRef), store them in a different type and convert the
667*67e74705SXin Li // container afterwards.
668*67e74705SXin Li std::string StorageType = getStorageType(getType());
669*67e74705SXin Li std::string StorageName = getLowerName();
670*67e74705SXin Li if (StorageType != getType()) {
671*67e74705SXin Li StorageName += "Storage";
672*67e74705SXin Li OS << " SmallVector<" << StorageType << ", 4> "
673*67e74705SXin Li << StorageName << ";\n";
674*67e74705SXin Li OS << " " << StorageName << ".reserve(" << getLowerName()
675*67e74705SXin Li << "Size);\n";
676*67e74705SXin Li }
677*67e74705SXin Li
678*67e74705SXin Li OS << " for (unsigned i = 0; i != " << getLowerName() << "Size; ++i)\n";
679*67e74705SXin Li std::string read = ReadPCHRecord(Type);
680*67e74705SXin Li OS << " " << StorageName << ".push_back(" << read << ");\n";
681*67e74705SXin Li
682*67e74705SXin Li if (StorageType != getType()) {
683*67e74705SXin Li OS << " for (unsigned i = 0; i != " << getLowerName() << "Size; ++i)\n";
684*67e74705SXin Li OS << " " << getLowerName() << ".push_back("
685*67e74705SXin Li << StorageName << "[i]);\n";
686*67e74705SXin Li }
687*67e74705SXin Li }
688*67e74705SXin Li
writePCHReadArgs(raw_ostream & OS) const689*67e74705SXin Li void writePCHReadArgs(raw_ostream &OS) const override {
690*67e74705SXin Li OS << getLowerName() << ".data(), " << getLowerName() << "Size";
691*67e74705SXin Li }
692*67e74705SXin Li
writePCHWrite(raw_ostream & OS) const693*67e74705SXin Li void writePCHWrite(raw_ostream &OS) const override {
694*67e74705SXin Li OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
695*67e74705SXin Li OS << " for (auto &Val : SA->" << RangeName << "())\n";
696*67e74705SXin Li OS << " " << WritePCHRecord(Type, "Val");
697*67e74705SXin Li }
698*67e74705SXin Li
writeValue(raw_ostream & OS) const699*67e74705SXin Li void writeValue(raw_ostream &OS) const override {
700*67e74705SXin Li OS << "\";\n";
701*67e74705SXin Li OS << " bool isFirst = true;\n"
702*67e74705SXin Li << " for (const auto &Val : " << RangeName << "()) {\n"
703*67e74705SXin Li << " if (isFirst) isFirst = false;\n"
704*67e74705SXin Li << " else OS << \", \";\n";
705*67e74705SXin Li writeValueImpl(OS);
706*67e74705SXin Li OS << " }\n";
707*67e74705SXin Li OS << " OS << \"";
708*67e74705SXin Li }
709*67e74705SXin Li
writeDump(raw_ostream & OS) const710*67e74705SXin Li void writeDump(raw_ostream &OS) const override {
711*67e74705SXin Li OS << " for (const auto &Val : SA->" << RangeName << "())\n";
712*67e74705SXin Li OS << " OS << \" \" << Val;\n";
713*67e74705SXin Li }
714*67e74705SXin Li };
715*67e74705SXin Li
716*67e74705SXin Li // Unique the enums, but maintain the original declaration ordering.
717*67e74705SXin Li std::vector<std::string>
uniqueEnumsInOrder(const std::vector<std::string> & enums)718*67e74705SXin Li uniqueEnumsInOrder(const std::vector<std::string> &enums) {
719*67e74705SXin Li std::vector<std::string> uniques;
720*67e74705SXin Li std::set<std::string> unique_set(enums.begin(), enums.end());
721*67e74705SXin Li for (const auto &i : enums) {
722*67e74705SXin Li auto set_i = unique_set.find(i);
723*67e74705SXin Li if (set_i != unique_set.end()) {
724*67e74705SXin Li uniques.push_back(i);
725*67e74705SXin Li unique_set.erase(set_i);
726*67e74705SXin Li }
727*67e74705SXin Li }
728*67e74705SXin Li return uniques;
729*67e74705SXin Li }
730*67e74705SXin Li
731*67e74705SXin Li class EnumArgument : public Argument {
732*67e74705SXin Li std::string type;
733*67e74705SXin Li std::vector<std::string> values, enums, uniques;
734*67e74705SXin Li public:
EnumArgument(const Record & Arg,StringRef Attr)735*67e74705SXin Li EnumArgument(const Record &Arg, StringRef Attr)
736*67e74705SXin Li : Argument(Arg, Attr), type(Arg.getValueAsString("Type")),
737*67e74705SXin Li values(Arg.getValueAsListOfStrings("Values")),
738*67e74705SXin Li enums(Arg.getValueAsListOfStrings("Enums")),
739*67e74705SXin Li uniques(uniqueEnumsInOrder(enums))
740*67e74705SXin Li {
741*67e74705SXin Li // FIXME: Emit a proper error
742*67e74705SXin Li assert(!uniques.empty());
743*67e74705SXin Li }
744*67e74705SXin Li
isEnumArg() const745*67e74705SXin Li bool isEnumArg() const override { return true; }
746*67e74705SXin Li
writeAccessors(raw_ostream & OS) const747*67e74705SXin Li void writeAccessors(raw_ostream &OS) const override {
748*67e74705SXin Li OS << " " << type << " get" << getUpperName() << "() const {\n";
749*67e74705SXin Li OS << " return " << getLowerName() << ";\n";
750*67e74705SXin Li OS << " }";
751*67e74705SXin Li }
752*67e74705SXin Li
writeCloneArgs(raw_ostream & OS) const753*67e74705SXin Li void writeCloneArgs(raw_ostream &OS) const override {
754*67e74705SXin Li OS << getLowerName();
755*67e74705SXin Li }
756*67e74705SXin Li
writeTemplateInstantiationArgs(raw_ostream & OS) const757*67e74705SXin Li void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
758*67e74705SXin Li OS << "A->get" << getUpperName() << "()";
759*67e74705SXin Li }
writeCtorInitializers(raw_ostream & OS) const760*67e74705SXin Li void writeCtorInitializers(raw_ostream &OS) const override {
761*67e74705SXin Li OS << getLowerName() << "(" << getUpperName() << ")";
762*67e74705SXin Li }
writeCtorDefaultInitializers(raw_ostream & OS) const763*67e74705SXin Li void writeCtorDefaultInitializers(raw_ostream &OS) const override {
764*67e74705SXin Li OS << getLowerName() << "(" << type << "(0))";
765*67e74705SXin Li }
writeCtorParameters(raw_ostream & OS) const766*67e74705SXin Li void writeCtorParameters(raw_ostream &OS) const override {
767*67e74705SXin Li OS << type << " " << getUpperName();
768*67e74705SXin Li }
writeDeclarations(raw_ostream & OS) const769*67e74705SXin Li void writeDeclarations(raw_ostream &OS) const override {
770*67e74705SXin Li auto i = uniques.cbegin(), e = uniques.cend();
771*67e74705SXin Li // The last one needs to not have a comma.
772*67e74705SXin Li --e;
773*67e74705SXin Li
774*67e74705SXin Li OS << "public:\n";
775*67e74705SXin Li OS << " enum " << type << " {\n";
776*67e74705SXin Li for (; i != e; ++i)
777*67e74705SXin Li OS << " " << *i << ",\n";
778*67e74705SXin Li OS << " " << *e << "\n";
779*67e74705SXin Li OS << " };\n";
780*67e74705SXin Li OS << "private:\n";
781*67e74705SXin Li OS << " " << type << " " << getLowerName() << ";";
782*67e74705SXin Li }
783*67e74705SXin Li
writePCHReadDecls(raw_ostream & OS) const784*67e74705SXin Li void writePCHReadDecls(raw_ostream &OS) const override {
785*67e74705SXin Li OS << " " << getAttrName() << "Attr::" << type << " " << getLowerName()
786*67e74705SXin Li << "(static_cast<" << getAttrName() << "Attr::" << type
787*67e74705SXin Li << ">(Record[Idx++]));\n";
788*67e74705SXin Li }
789*67e74705SXin Li
writePCHReadArgs(raw_ostream & OS) const790*67e74705SXin Li void writePCHReadArgs(raw_ostream &OS) const override {
791*67e74705SXin Li OS << getLowerName();
792*67e74705SXin Li }
793*67e74705SXin Li
writePCHWrite(raw_ostream & OS) const794*67e74705SXin Li void writePCHWrite(raw_ostream &OS) const override {
795*67e74705SXin Li OS << "Record.push_back(SA->get" << getUpperName() << "());\n";
796*67e74705SXin Li }
797*67e74705SXin Li
writeValue(raw_ostream & OS) const798*67e74705SXin Li void writeValue(raw_ostream &OS) const override {
799*67e74705SXin Li // FIXME: this isn't 100% correct -- some enum arguments require printing
800*67e74705SXin Li // as a string literal, while others require printing as an identifier.
801*67e74705SXin Li // Tablegen currently does not distinguish between the two forms.
802*67e74705SXin Li OS << "\\\"\" << " << getAttrName() << "Attr::Convert" << type << "ToStr(get"
803*67e74705SXin Li << getUpperName() << "()) << \"\\\"";
804*67e74705SXin Li }
805*67e74705SXin Li
writeDump(raw_ostream & OS) const806*67e74705SXin Li void writeDump(raw_ostream &OS) const override {
807*67e74705SXin Li OS << " switch(SA->get" << getUpperName() << "()) {\n";
808*67e74705SXin Li for (const auto &I : uniques) {
809*67e74705SXin Li OS << " case " << getAttrName() << "Attr::" << I << ":\n";
810*67e74705SXin Li OS << " OS << \" " << I << "\";\n";
811*67e74705SXin Li OS << " break;\n";
812*67e74705SXin Li }
813*67e74705SXin Li OS << " }\n";
814*67e74705SXin Li }
815*67e74705SXin Li
writeConversion(raw_ostream & OS) const816*67e74705SXin Li void writeConversion(raw_ostream &OS) const {
817*67e74705SXin Li OS << " static bool ConvertStrTo" << type << "(StringRef Val, ";
818*67e74705SXin Li OS << type << " &Out) {\n";
819*67e74705SXin Li OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<";
820*67e74705SXin Li OS << type << ">>(Val)\n";
821*67e74705SXin Li for (size_t I = 0; I < enums.size(); ++I) {
822*67e74705SXin Li OS << " .Case(\"" << values[I] << "\", ";
823*67e74705SXin Li OS << getAttrName() << "Attr::" << enums[I] << ")\n";
824*67e74705SXin Li }
825*67e74705SXin Li OS << " .Default(Optional<" << type << ">());\n";
826*67e74705SXin Li OS << " if (R) {\n";
827*67e74705SXin Li OS << " Out = *R;\n return true;\n }\n";
828*67e74705SXin Li OS << " return false;\n";
829*67e74705SXin Li OS << " }\n\n";
830*67e74705SXin Li
831*67e74705SXin Li // Mapping from enumeration values back to enumeration strings isn't
832*67e74705SXin Li // trivial because some enumeration values have multiple named
833*67e74705SXin Li // enumerators, such as type_visibility(internal) and
834*67e74705SXin Li // type_visibility(hidden) both mapping to TypeVisibilityAttr::Hidden.
835*67e74705SXin Li OS << " static const char *Convert" << type << "ToStr("
836*67e74705SXin Li << type << " Val) {\n"
837*67e74705SXin Li << " switch(Val) {\n";
838*67e74705SXin Li std::set<std::string> Uniques;
839*67e74705SXin Li for (size_t I = 0; I < enums.size(); ++I) {
840*67e74705SXin Li if (Uniques.insert(enums[I]).second)
841*67e74705SXin Li OS << " case " << getAttrName() << "Attr::" << enums[I]
842*67e74705SXin Li << ": return \"" << values[I] << "\";\n";
843*67e74705SXin Li }
844*67e74705SXin Li OS << " }\n"
845*67e74705SXin Li << " llvm_unreachable(\"No enumerator with that value\");\n"
846*67e74705SXin Li << " }\n";
847*67e74705SXin Li }
848*67e74705SXin Li };
849*67e74705SXin Li
850*67e74705SXin Li class VariadicEnumArgument: public VariadicArgument {
851*67e74705SXin Li std::string type, QualifiedTypeName;
852*67e74705SXin Li std::vector<std::string> values, enums, uniques;
853*67e74705SXin Li
854*67e74705SXin Li protected:
writeValueImpl(raw_ostream & OS) const855*67e74705SXin Li void writeValueImpl(raw_ostream &OS) const override {
856*67e74705SXin Li // FIXME: this isn't 100% correct -- some enum arguments require printing
857*67e74705SXin Li // as a string literal, while others require printing as an identifier.
858*67e74705SXin Li // Tablegen currently does not distinguish between the two forms.
859*67e74705SXin Li OS << " OS << \"\\\"\" << " << getAttrName() << "Attr::Convert" << type
860*67e74705SXin Li << "ToStr(Val)" << "<< \"\\\"\";\n";
861*67e74705SXin Li }
862*67e74705SXin Li
863*67e74705SXin Li public:
VariadicEnumArgument(const Record & Arg,StringRef Attr)864*67e74705SXin Li VariadicEnumArgument(const Record &Arg, StringRef Attr)
865*67e74705SXin Li : VariadicArgument(Arg, Attr, Arg.getValueAsString("Type")),
866*67e74705SXin Li type(Arg.getValueAsString("Type")),
867*67e74705SXin Li values(Arg.getValueAsListOfStrings("Values")),
868*67e74705SXin Li enums(Arg.getValueAsListOfStrings("Enums")),
869*67e74705SXin Li uniques(uniqueEnumsInOrder(enums))
870*67e74705SXin Li {
871*67e74705SXin Li QualifiedTypeName = getAttrName().str() + "Attr::" + type;
872*67e74705SXin Li
873*67e74705SXin Li // FIXME: Emit a proper error
874*67e74705SXin Li assert(!uniques.empty());
875*67e74705SXin Li }
876*67e74705SXin Li
isVariadicEnumArg() const877*67e74705SXin Li bool isVariadicEnumArg() const override { return true; }
878*67e74705SXin Li
writeDeclarations(raw_ostream & OS) const879*67e74705SXin Li void writeDeclarations(raw_ostream &OS) const override {
880*67e74705SXin Li auto i = uniques.cbegin(), e = uniques.cend();
881*67e74705SXin Li // The last one needs to not have a comma.
882*67e74705SXin Li --e;
883*67e74705SXin Li
884*67e74705SXin Li OS << "public:\n";
885*67e74705SXin Li OS << " enum " << type << " {\n";
886*67e74705SXin Li for (; i != e; ++i)
887*67e74705SXin Li OS << " " << *i << ",\n";
888*67e74705SXin Li OS << " " << *e << "\n";
889*67e74705SXin Li OS << " };\n";
890*67e74705SXin Li OS << "private:\n";
891*67e74705SXin Li
892*67e74705SXin Li VariadicArgument::writeDeclarations(OS);
893*67e74705SXin Li }
894*67e74705SXin Li
writeDump(raw_ostream & OS) const895*67e74705SXin Li void writeDump(raw_ostream &OS) const override {
896*67e74705SXin Li OS << " for (" << getAttrName() << "Attr::" << getLowerName()
897*67e74705SXin Li << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
898*67e74705SXin Li << getLowerName() << "_end(); I != E; ++I) {\n";
899*67e74705SXin Li OS << " switch(*I) {\n";
900*67e74705SXin Li for (const auto &UI : uniques) {
901*67e74705SXin Li OS << " case " << getAttrName() << "Attr::" << UI << ":\n";
902*67e74705SXin Li OS << " OS << \" " << UI << "\";\n";
903*67e74705SXin Li OS << " break;\n";
904*67e74705SXin Li }
905*67e74705SXin Li OS << " }\n";
906*67e74705SXin Li OS << " }\n";
907*67e74705SXin Li }
908*67e74705SXin Li
writePCHReadDecls(raw_ostream & OS) const909*67e74705SXin Li void writePCHReadDecls(raw_ostream &OS) const override {
910*67e74705SXin Li OS << " unsigned " << getLowerName() << "Size = Record[Idx++];\n";
911*67e74705SXin Li OS << " SmallVector<" << QualifiedTypeName << ", 4> " << getLowerName()
912*67e74705SXin Li << ";\n";
913*67e74705SXin Li OS << " " << getLowerName() << ".reserve(" << getLowerName()
914*67e74705SXin Li << "Size);\n";
915*67e74705SXin Li OS << " for (unsigned i = " << getLowerName() << "Size; i; --i)\n";
916*67e74705SXin Li OS << " " << getLowerName() << ".push_back(" << "static_cast<"
917*67e74705SXin Li << QualifiedTypeName << ">(Record[Idx++]));\n";
918*67e74705SXin Li }
919*67e74705SXin Li
writePCHWrite(raw_ostream & OS) const920*67e74705SXin Li void writePCHWrite(raw_ostream &OS) const override {
921*67e74705SXin Li OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
922*67e74705SXin Li OS << " for (" << getAttrName() << "Attr::" << getLowerName()
923*67e74705SXin Li << "_iterator i = SA->" << getLowerName() << "_begin(), e = SA->"
924*67e74705SXin Li << getLowerName() << "_end(); i != e; ++i)\n";
925*67e74705SXin Li OS << " " << WritePCHRecord(QualifiedTypeName, "(*i)");
926*67e74705SXin Li }
927*67e74705SXin Li
writeConversion(raw_ostream & OS) const928*67e74705SXin Li void writeConversion(raw_ostream &OS) const {
929*67e74705SXin Li OS << " static bool ConvertStrTo" << type << "(StringRef Val, ";
930*67e74705SXin Li OS << type << " &Out) {\n";
931*67e74705SXin Li OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<";
932*67e74705SXin Li OS << type << ">>(Val)\n";
933*67e74705SXin Li for (size_t I = 0; I < enums.size(); ++I) {
934*67e74705SXin Li OS << " .Case(\"" << values[I] << "\", ";
935*67e74705SXin Li OS << getAttrName() << "Attr::" << enums[I] << ")\n";
936*67e74705SXin Li }
937*67e74705SXin Li OS << " .Default(Optional<" << type << ">());\n";
938*67e74705SXin Li OS << " if (R) {\n";
939*67e74705SXin Li OS << " Out = *R;\n return true;\n }\n";
940*67e74705SXin Li OS << " return false;\n";
941*67e74705SXin Li OS << " }\n\n";
942*67e74705SXin Li
943*67e74705SXin Li OS << " static const char *Convert" << type << "ToStr("
944*67e74705SXin Li << type << " Val) {\n"
945*67e74705SXin Li << " switch(Val) {\n";
946*67e74705SXin Li std::set<std::string> Uniques;
947*67e74705SXin Li for (size_t I = 0; I < enums.size(); ++I) {
948*67e74705SXin Li if (Uniques.insert(enums[I]).second)
949*67e74705SXin Li OS << " case " << getAttrName() << "Attr::" << enums[I]
950*67e74705SXin Li << ": return \"" << values[I] << "\";\n";
951*67e74705SXin Li }
952*67e74705SXin Li OS << " }\n"
953*67e74705SXin Li << " llvm_unreachable(\"No enumerator with that value\");\n"
954*67e74705SXin Li << " }\n";
955*67e74705SXin Li }
956*67e74705SXin Li };
957*67e74705SXin Li
958*67e74705SXin Li class VersionArgument : public Argument {
959*67e74705SXin Li public:
VersionArgument(const Record & Arg,StringRef Attr)960*67e74705SXin Li VersionArgument(const Record &Arg, StringRef Attr)
961*67e74705SXin Li : Argument(Arg, Attr)
962*67e74705SXin Li {}
963*67e74705SXin Li
writeAccessors(raw_ostream & OS) const964*67e74705SXin Li void writeAccessors(raw_ostream &OS) const override {
965*67e74705SXin Li OS << " VersionTuple get" << getUpperName() << "() const {\n";
966*67e74705SXin Li OS << " return " << getLowerName() << ";\n";
967*67e74705SXin Li OS << " }\n";
968*67e74705SXin Li OS << " void set" << getUpperName()
969*67e74705SXin Li << "(ASTContext &C, VersionTuple V) {\n";
970*67e74705SXin Li OS << " " << getLowerName() << " = V;\n";
971*67e74705SXin Li OS << " }";
972*67e74705SXin Li }
973*67e74705SXin Li
writeCloneArgs(raw_ostream & OS) const974*67e74705SXin Li void writeCloneArgs(raw_ostream &OS) const override {
975*67e74705SXin Li OS << "get" << getUpperName() << "()";
976*67e74705SXin Li }
977*67e74705SXin Li
writeTemplateInstantiationArgs(raw_ostream & OS) const978*67e74705SXin Li void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
979*67e74705SXin Li OS << "A->get" << getUpperName() << "()";
980*67e74705SXin Li }
981*67e74705SXin Li
writeCtorInitializers(raw_ostream & OS) const982*67e74705SXin Li void writeCtorInitializers(raw_ostream &OS) const override {
983*67e74705SXin Li OS << getLowerName() << "(" << getUpperName() << ")";
984*67e74705SXin Li }
985*67e74705SXin Li
writeCtorDefaultInitializers(raw_ostream & OS) const986*67e74705SXin Li void writeCtorDefaultInitializers(raw_ostream &OS) const override {
987*67e74705SXin Li OS << getLowerName() << "()";
988*67e74705SXin Li }
989*67e74705SXin Li
writeCtorParameters(raw_ostream & OS) const990*67e74705SXin Li void writeCtorParameters(raw_ostream &OS) const override {
991*67e74705SXin Li OS << "VersionTuple " << getUpperName();
992*67e74705SXin Li }
993*67e74705SXin Li
writeDeclarations(raw_ostream & OS) const994*67e74705SXin Li void writeDeclarations(raw_ostream &OS) const override {
995*67e74705SXin Li OS << "VersionTuple " << getLowerName() << ";\n";
996*67e74705SXin Li }
997*67e74705SXin Li
writePCHReadDecls(raw_ostream & OS) const998*67e74705SXin Li void writePCHReadDecls(raw_ostream &OS) const override {
999*67e74705SXin Li OS << " VersionTuple " << getLowerName()
1000*67e74705SXin Li << "= ReadVersionTuple(Record, Idx);\n";
1001*67e74705SXin Li }
1002*67e74705SXin Li
writePCHReadArgs(raw_ostream & OS) const1003*67e74705SXin Li void writePCHReadArgs(raw_ostream &OS) const override {
1004*67e74705SXin Li OS << getLowerName();
1005*67e74705SXin Li }
1006*67e74705SXin Li
writePCHWrite(raw_ostream & OS) const1007*67e74705SXin Li void writePCHWrite(raw_ostream &OS) const override {
1008*67e74705SXin Li OS << " Record.AddVersionTuple(SA->get" << getUpperName() << "());\n";
1009*67e74705SXin Li }
1010*67e74705SXin Li
writeValue(raw_ostream & OS) const1011*67e74705SXin Li void writeValue(raw_ostream &OS) const override {
1012*67e74705SXin Li OS << getLowerName() << "=\" << get" << getUpperName() << "() << \"";
1013*67e74705SXin Li }
1014*67e74705SXin Li
writeDump(raw_ostream & OS) const1015*67e74705SXin Li void writeDump(raw_ostream &OS) const override {
1016*67e74705SXin Li OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
1017*67e74705SXin Li }
1018*67e74705SXin Li };
1019*67e74705SXin Li
1020*67e74705SXin Li class ExprArgument : public SimpleArgument {
1021*67e74705SXin Li public:
ExprArgument(const Record & Arg,StringRef Attr)1022*67e74705SXin Li ExprArgument(const Record &Arg, StringRef Attr)
1023*67e74705SXin Li : SimpleArgument(Arg, Attr, "Expr *")
1024*67e74705SXin Li {}
1025*67e74705SXin Li
writeASTVisitorTraversal(raw_ostream & OS) const1026*67e74705SXin Li void writeASTVisitorTraversal(raw_ostream &OS) const override {
1027*67e74705SXin Li OS << " if (!"
1028*67e74705SXin Li << "getDerived().TraverseStmt(A->get" << getUpperName() << "()))\n";
1029*67e74705SXin Li OS << " return false;\n";
1030*67e74705SXin Li }
1031*67e74705SXin Li
writeTemplateInstantiationArgs(raw_ostream & OS) const1032*67e74705SXin Li void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
1033*67e74705SXin Li OS << "tempInst" << getUpperName();
1034*67e74705SXin Li }
1035*67e74705SXin Li
writeTemplateInstantiation(raw_ostream & OS) const1036*67e74705SXin Li void writeTemplateInstantiation(raw_ostream &OS) const override {
1037*67e74705SXin Li OS << " " << getType() << " tempInst" << getUpperName() << ";\n";
1038*67e74705SXin Li OS << " {\n";
1039*67e74705SXin Li OS << " EnterExpressionEvaluationContext "
1040*67e74705SXin Li << "Unevaluated(S, Sema::Unevaluated);\n";
1041*67e74705SXin Li OS << " ExprResult " << "Result = S.SubstExpr("
1042*67e74705SXin Li << "A->get" << getUpperName() << "(), TemplateArgs);\n";
1043*67e74705SXin Li OS << " tempInst" << getUpperName() << " = "
1044*67e74705SXin Li << "Result.getAs<Expr>();\n";
1045*67e74705SXin Li OS << " }\n";
1046*67e74705SXin Li }
1047*67e74705SXin Li
writeDump(raw_ostream & OS) const1048*67e74705SXin Li void writeDump(raw_ostream &OS) const override {}
1049*67e74705SXin Li
writeDumpChildren(raw_ostream & OS) const1050*67e74705SXin Li void writeDumpChildren(raw_ostream &OS) const override {
1051*67e74705SXin Li OS << " dumpStmt(SA->get" << getUpperName() << "());\n";
1052*67e74705SXin Li }
1053*67e74705SXin Li
writeHasChildren(raw_ostream & OS) const1054*67e74705SXin Li void writeHasChildren(raw_ostream &OS) const override { OS << "true"; }
1055*67e74705SXin Li };
1056*67e74705SXin Li
1057*67e74705SXin Li class VariadicExprArgument : public VariadicArgument {
1058*67e74705SXin Li public:
VariadicExprArgument(const Record & Arg,StringRef Attr)1059*67e74705SXin Li VariadicExprArgument(const Record &Arg, StringRef Attr)
1060*67e74705SXin Li : VariadicArgument(Arg, Attr, "Expr *")
1061*67e74705SXin Li {}
1062*67e74705SXin Li
writeASTVisitorTraversal(raw_ostream & OS) const1063*67e74705SXin Li void writeASTVisitorTraversal(raw_ostream &OS) const override {
1064*67e74705SXin Li OS << " {\n";
1065*67e74705SXin Li OS << " " << getType() << " *I = A->" << getLowerName()
1066*67e74705SXin Li << "_begin();\n";
1067*67e74705SXin Li OS << " " << getType() << " *E = A->" << getLowerName()
1068*67e74705SXin Li << "_end();\n";
1069*67e74705SXin Li OS << " for (; I != E; ++I) {\n";
1070*67e74705SXin Li OS << " if (!getDerived().TraverseStmt(*I))\n";
1071*67e74705SXin Li OS << " return false;\n";
1072*67e74705SXin Li OS << " }\n";
1073*67e74705SXin Li OS << " }\n";
1074*67e74705SXin Li }
1075*67e74705SXin Li
writeTemplateInstantiationArgs(raw_ostream & OS) const1076*67e74705SXin Li void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
1077*67e74705SXin Li OS << "tempInst" << getUpperName() << ", "
1078*67e74705SXin Li << "A->" << getLowerName() << "_size()";
1079*67e74705SXin Li }
1080*67e74705SXin Li
writeTemplateInstantiation(raw_ostream & OS) const1081*67e74705SXin Li void writeTemplateInstantiation(raw_ostream &OS) const override {
1082*67e74705SXin Li OS << " auto *tempInst" << getUpperName()
1083*67e74705SXin Li << " = new (C, 16) " << getType()
1084*67e74705SXin Li << "[A->" << getLowerName() << "_size()];\n";
1085*67e74705SXin Li OS << " {\n";
1086*67e74705SXin Li OS << " EnterExpressionEvaluationContext "
1087*67e74705SXin Li << "Unevaluated(S, Sema::Unevaluated);\n";
1088*67e74705SXin Li OS << " " << getType() << " *TI = tempInst" << getUpperName()
1089*67e74705SXin Li << ";\n";
1090*67e74705SXin Li OS << " " << getType() << " *I = A->" << getLowerName()
1091*67e74705SXin Li << "_begin();\n";
1092*67e74705SXin Li OS << " " << getType() << " *E = A->" << getLowerName()
1093*67e74705SXin Li << "_end();\n";
1094*67e74705SXin Li OS << " for (; I != E; ++I, ++TI) {\n";
1095*67e74705SXin Li OS << " ExprResult Result = S.SubstExpr(*I, TemplateArgs);\n";
1096*67e74705SXin Li OS << " *TI = Result.getAs<Expr>();\n";
1097*67e74705SXin Li OS << " }\n";
1098*67e74705SXin Li OS << " }\n";
1099*67e74705SXin Li }
1100*67e74705SXin Li
writeDump(raw_ostream & OS) const1101*67e74705SXin Li void writeDump(raw_ostream &OS) const override {}
1102*67e74705SXin Li
writeDumpChildren(raw_ostream & OS) const1103*67e74705SXin Li void writeDumpChildren(raw_ostream &OS) const override {
1104*67e74705SXin Li OS << " for (" << getAttrName() << "Attr::" << getLowerName()
1105*67e74705SXin Li << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
1106*67e74705SXin Li << getLowerName() << "_end(); I != E; ++I)\n";
1107*67e74705SXin Li OS << " dumpStmt(*I);\n";
1108*67e74705SXin Li }
1109*67e74705SXin Li
writeHasChildren(raw_ostream & OS) const1110*67e74705SXin Li void writeHasChildren(raw_ostream &OS) const override {
1111*67e74705SXin Li OS << "SA->" << getLowerName() << "_begin() != "
1112*67e74705SXin Li << "SA->" << getLowerName() << "_end()";
1113*67e74705SXin Li }
1114*67e74705SXin Li };
1115*67e74705SXin Li
1116*67e74705SXin Li class VariadicStringArgument : public VariadicArgument {
1117*67e74705SXin Li public:
VariadicStringArgument(const Record & Arg,StringRef Attr)1118*67e74705SXin Li VariadicStringArgument(const Record &Arg, StringRef Attr)
1119*67e74705SXin Li : VariadicArgument(Arg, Attr, "StringRef")
1120*67e74705SXin Li {}
1121*67e74705SXin Li
writeCtorBody(raw_ostream & OS) const1122*67e74705SXin Li void writeCtorBody(raw_ostream &OS) const override {
1123*67e74705SXin Li OS << " for (size_t I = 0, E = " << getArgSizeName() << "; I != E;\n"
1124*67e74705SXin Li " ++I) {\n"
1125*67e74705SXin Li " StringRef Ref = " << getUpperName() << "[I];\n"
1126*67e74705SXin Li " if (!Ref.empty()) {\n"
1127*67e74705SXin Li " char *Mem = new (Ctx, 1) char[Ref.size()];\n"
1128*67e74705SXin Li " std::memcpy(Mem, Ref.data(), Ref.size());\n"
1129*67e74705SXin Li " " << getArgName() << "[I] = StringRef(Mem, Ref.size());\n"
1130*67e74705SXin Li " }\n"
1131*67e74705SXin Li " }\n";
1132*67e74705SXin Li }
1133*67e74705SXin Li
writeValueImpl(raw_ostream & OS) const1134*67e74705SXin Li void writeValueImpl(raw_ostream &OS) const override {
1135*67e74705SXin Li OS << " OS << \"\\\"\" << Val << \"\\\"\";\n";
1136*67e74705SXin Li }
1137*67e74705SXin Li };
1138*67e74705SXin Li
1139*67e74705SXin Li class TypeArgument : public SimpleArgument {
1140*67e74705SXin Li public:
TypeArgument(const Record & Arg,StringRef Attr)1141*67e74705SXin Li TypeArgument(const Record &Arg, StringRef Attr)
1142*67e74705SXin Li : SimpleArgument(Arg, Attr, "TypeSourceInfo *")
1143*67e74705SXin Li {}
1144*67e74705SXin Li
writeAccessors(raw_ostream & OS) const1145*67e74705SXin Li void writeAccessors(raw_ostream &OS) const override {
1146*67e74705SXin Li OS << " QualType get" << getUpperName() << "() const {\n";
1147*67e74705SXin Li OS << " return " << getLowerName() << "->getType();\n";
1148*67e74705SXin Li OS << " }";
1149*67e74705SXin Li OS << " " << getType() << " get" << getUpperName() << "Loc() const {\n";
1150*67e74705SXin Li OS << " return " << getLowerName() << ";\n";
1151*67e74705SXin Li OS << " }";
1152*67e74705SXin Li }
1153*67e74705SXin Li
writeTemplateInstantiationArgs(raw_ostream & OS) const1154*67e74705SXin Li void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
1155*67e74705SXin Li OS << "A->get" << getUpperName() << "Loc()";
1156*67e74705SXin Li }
1157*67e74705SXin Li
writePCHWrite(raw_ostream & OS) const1158*67e74705SXin Li void writePCHWrite(raw_ostream &OS) const override {
1159*67e74705SXin Li OS << " " << WritePCHRecord(
1160*67e74705SXin Li getType(), "SA->get" + std::string(getUpperName()) + "Loc()");
1161*67e74705SXin Li }
1162*67e74705SXin Li };
1163*67e74705SXin Li
1164*67e74705SXin Li } // end anonymous namespace
1165*67e74705SXin Li
1166*67e74705SXin Li static std::unique_ptr<Argument>
createArgument(const Record & Arg,StringRef Attr,const Record * Search=nullptr)1167*67e74705SXin Li createArgument(const Record &Arg, StringRef Attr,
1168*67e74705SXin Li const Record *Search = nullptr) {
1169*67e74705SXin Li if (!Search)
1170*67e74705SXin Li Search = &Arg;
1171*67e74705SXin Li
1172*67e74705SXin Li std::unique_ptr<Argument> Ptr;
1173*67e74705SXin Li llvm::StringRef ArgName = Search->getName();
1174*67e74705SXin Li
1175*67e74705SXin Li if (ArgName == "AlignedArgument")
1176*67e74705SXin Li Ptr = llvm::make_unique<AlignedArgument>(Arg, Attr);
1177*67e74705SXin Li else if (ArgName == "EnumArgument")
1178*67e74705SXin Li Ptr = llvm::make_unique<EnumArgument>(Arg, Attr);
1179*67e74705SXin Li else if (ArgName == "ExprArgument")
1180*67e74705SXin Li Ptr = llvm::make_unique<ExprArgument>(Arg, Attr);
1181*67e74705SXin Li else if (ArgName == "FunctionArgument")
1182*67e74705SXin Li Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "FunctionDecl *");
1183*67e74705SXin Li else if (ArgName == "IdentifierArgument")
1184*67e74705SXin Li Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "IdentifierInfo *");
1185*67e74705SXin Li else if (ArgName == "DefaultBoolArgument")
1186*67e74705SXin Li Ptr = llvm::make_unique<DefaultSimpleArgument>(
1187*67e74705SXin Li Arg, Attr, "bool", Arg.getValueAsBit("Default"));
1188*67e74705SXin Li else if (ArgName == "BoolArgument")
1189*67e74705SXin Li Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "bool");
1190*67e74705SXin Li else if (ArgName == "DefaultIntArgument")
1191*67e74705SXin Li Ptr = llvm::make_unique<DefaultSimpleArgument>(
1192*67e74705SXin Li Arg, Attr, "int", Arg.getValueAsInt("Default"));
1193*67e74705SXin Li else if (ArgName == "IntArgument")
1194*67e74705SXin Li Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "int");
1195*67e74705SXin Li else if (ArgName == "StringArgument")
1196*67e74705SXin Li Ptr = llvm::make_unique<StringArgument>(Arg, Attr);
1197*67e74705SXin Li else if (ArgName == "TypeArgument")
1198*67e74705SXin Li Ptr = llvm::make_unique<TypeArgument>(Arg, Attr);
1199*67e74705SXin Li else if (ArgName == "UnsignedArgument")
1200*67e74705SXin Li Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "unsigned");
1201*67e74705SXin Li else if (ArgName == "VariadicUnsignedArgument")
1202*67e74705SXin Li Ptr = llvm::make_unique<VariadicArgument>(Arg, Attr, "unsigned");
1203*67e74705SXin Li else if (ArgName == "VariadicStringArgument")
1204*67e74705SXin Li Ptr = llvm::make_unique<VariadicStringArgument>(Arg, Attr);
1205*67e74705SXin Li else if (ArgName == "VariadicEnumArgument")
1206*67e74705SXin Li Ptr = llvm::make_unique<VariadicEnumArgument>(Arg, Attr);
1207*67e74705SXin Li else if (ArgName == "VariadicExprArgument")
1208*67e74705SXin Li Ptr = llvm::make_unique<VariadicExprArgument>(Arg, Attr);
1209*67e74705SXin Li else if (ArgName == "VersionArgument")
1210*67e74705SXin Li Ptr = llvm::make_unique<VersionArgument>(Arg, Attr);
1211*67e74705SXin Li
1212*67e74705SXin Li if (!Ptr) {
1213*67e74705SXin Li // Search in reverse order so that the most-derived type is handled first.
1214*67e74705SXin Li ArrayRef<std::pair<Record*, SMRange>> Bases = Search->getSuperClasses();
1215*67e74705SXin Li for (const auto &Base : llvm::reverse(Bases)) {
1216*67e74705SXin Li if ((Ptr = createArgument(Arg, Attr, Base.first)))
1217*67e74705SXin Li break;
1218*67e74705SXin Li }
1219*67e74705SXin Li }
1220*67e74705SXin Li
1221*67e74705SXin Li if (Ptr && Arg.getValueAsBit("Optional"))
1222*67e74705SXin Li Ptr->setOptional(true);
1223*67e74705SXin Li
1224*67e74705SXin Li if (Ptr && Arg.getValueAsBit("Fake"))
1225*67e74705SXin Li Ptr->setFake(true);
1226*67e74705SXin Li
1227*67e74705SXin Li return Ptr;
1228*67e74705SXin Li }
1229*67e74705SXin Li
writeAvailabilityValue(raw_ostream & OS)1230*67e74705SXin Li static void writeAvailabilityValue(raw_ostream &OS) {
1231*67e74705SXin Li OS << "\" << getPlatform()->getName();\n"
1232*67e74705SXin Li << " if (getStrict()) OS << \", strict\";\n"
1233*67e74705SXin Li << " if (!getIntroduced().empty()) OS << \", introduced=\" << getIntroduced();\n"
1234*67e74705SXin Li << " if (!getDeprecated().empty()) OS << \", deprecated=\" << getDeprecated();\n"
1235*67e74705SXin Li << " if (!getObsoleted().empty()) OS << \", obsoleted=\" << getObsoleted();\n"
1236*67e74705SXin Li << " if (getUnavailable()) OS << \", unavailable\";\n"
1237*67e74705SXin Li << " OS << \"";
1238*67e74705SXin Li }
1239*67e74705SXin Li
writeDeprecatedAttrValue(raw_ostream & OS,std::string & Variety)1240*67e74705SXin Li static void writeDeprecatedAttrValue(raw_ostream &OS, std::string &Variety) {
1241*67e74705SXin Li OS << "\\\"\" << getMessage() << \"\\\"\";\n";
1242*67e74705SXin Li // Only GNU deprecated has an optional fixit argument at the second position.
1243*67e74705SXin Li if (Variety == "GNU")
1244*67e74705SXin Li OS << " if (!getReplacement().empty()) OS << \", \\\"\""
1245*67e74705SXin Li " << getReplacement() << \"\\\"\";\n";
1246*67e74705SXin Li OS << " OS << \"";
1247*67e74705SXin Li }
1248*67e74705SXin Li
writeGetSpellingFunction(Record & R,raw_ostream & OS)1249*67e74705SXin Li static void writeGetSpellingFunction(Record &R, raw_ostream &OS) {
1250*67e74705SXin Li std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
1251*67e74705SXin Li
1252*67e74705SXin Li OS << "const char *" << R.getName() << "Attr::getSpelling() const {\n";
1253*67e74705SXin Li if (Spellings.empty()) {
1254*67e74705SXin Li OS << " return \"(No spelling)\";\n}\n\n";
1255*67e74705SXin Li return;
1256*67e74705SXin Li }
1257*67e74705SXin Li
1258*67e74705SXin Li OS << " switch (SpellingListIndex) {\n"
1259*67e74705SXin Li " default:\n"
1260*67e74705SXin Li " llvm_unreachable(\"Unknown attribute spelling!\");\n"
1261*67e74705SXin Li " return \"(No spelling)\";\n";
1262*67e74705SXin Li
1263*67e74705SXin Li for (unsigned I = 0; I < Spellings.size(); ++I)
1264*67e74705SXin Li OS << " case " << I << ":\n"
1265*67e74705SXin Li " return \"" << Spellings[I].name() << "\";\n";
1266*67e74705SXin Li // End of the switch statement.
1267*67e74705SXin Li OS << " }\n";
1268*67e74705SXin Li // End of the getSpelling function.
1269*67e74705SXin Li OS << "}\n\n";
1270*67e74705SXin Li }
1271*67e74705SXin Li
1272*67e74705SXin Li static void
writePrettyPrintFunction(Record & R,const std::vector<std::unique_ptr<Argument>> & Args,raw_ostream & OS)1273*67e74705SXin Li writePrettyPrintFunction(Record &R,
1274*67e74705SXin Li const std::vector<std::unique_ptr<Argument>> &Args,
1275*67e74705SXin Li raw_ostream &OS) {
1276*67e74705SXin Li std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
1277*67e74705SXin Li
1278*67e74705SXin Li OS << "void " << R.getName() << "Attr::printPretty("
1279*67e74705SXin Li << "raw_ostream &OS, const PrintingPolicy &Policy) const {\n";
1280*67e74705SXin Li
1281*67e74705SXin Li if (Spellings.empty()) {
1282*67e74705SXin Li OS << "}\n\n";
1283*67e74705SXin Li return;
1284*67e74705SXin Li }
1285*67e74705SXin Li
1286*67e74705SXin Li OS <<
1287*67e74705SXin Li " switch (SpellingListIndex) {\n"
1288*67e74705SXin Li " default:\n"
1289*67e74705SXin Li " llvm_unreachable(\"Unknown attribute spelling!\");\n"
1290*67e74705SXin Li " break;\n";
1291*67e74705SXin Li
1292*67e74705SXin Li for (unsigned I = 0; I < Spellings.size(); ++ I) {
1293*67e74705SXin Li llvm::SmallString<16> Prefix;
1294*67e74705SXin Li llvm::SmallString<8> Suffix;
1295*67e74705SXin Li // The actual spelling of the name and namespace (if applicable)
1296*67e74705SXin Li // of an attribute without considering prefix and suffix.
1297*67e74705SXin Li llvm::SmallString<64> Spelling;
1298*67e74705SXin Li std::string Name = Spellings[I].name();
1299*67e74705SXin Li std::string Variety = Spellings[I].variety();
1300*67e74705SXin Li
1301*67e74705SXin Li if (Variety == "GNU") {
1302*67e74705SXin Li Prefix = " __attribute__((";
1303*67e74705SXin Li Suffix = "))";
1304*67e74705SXin Li } else if (Variety == "CXX11") {
1305*67e74705SXin Li Prefix = " [[";
1306*67e74705SXin Li Suffix = "]]";
1307*67e74705SXin Li std::string Namespace = Spellings[I].nameSpace();
1308*67e74705SXin Li if (!Namespace.empty()) {
1309*67e74705SXin Li Spelling += Namespace;
1310*67e74705SXin Li Spelling += "::";
1311*67e74705SXin Li }
1312*67e74705SXin Li } else if (Variety == "Declspec") {
1313*67e74705SXin Li Prefix = " __declspec(";
1314*67e74705SXin Li Suffix = ")";
1315*67e74705SXin Li } else if (Variety == "Keyword") {
1316*67e74705SXin Li Prefix = " ";
1317*67e74705SXin Li Suffix = "";
1318*67e74705SXin Li } else if (Variety == "Pragma") {
1319*67e74705SXin Li Prefix = "#pragma ";
1320*67e74705SXin Li Suffix = "\n";
1321*67e74705SXin Li std::string Namespace = Spellings[I].nameSpace();
1322*67e74705SXin Li if (!Namespace.empty()) {
1323*67e74705SXin Li Spelling += Namespace;
1324*67e74705SXin Li Spelling += " ";
1325*67e74705SXin Li }
1326*67e74705SXin Li } else {
1327*67e74705SXin Li llvm_unreachable("Unknown attribute syntax variety!");
1328*67e74705SXin Li }
1329*67e74705SXin Li
1330*67e74705SXin Li Spelling += Name;
1331*67e74705SXin Li
1332*67e74705SXin Li OS <<
1333*67e74705SXin Li " case " << I << " : {\n"
1334*67e74705SXin Li " OS << \"" << Prefix << Spelling;
1335*67e74705SXin Li
1336*67e74705SXin Li if (Variety == "Pragma") {
1337*67e74705SXin Li OS << " \";\n";
1338*67e74705SXin Li OS << " printPrettyPragma(OS, Policy);\n";
1339*67e74705SXin Li OS << " OS << \"\\n\";";
1340*67e74705SXin Li OS << " break;\n";
1341*67e74705SXin Li OS << " }\n";
1342*67e74705SXin Li continue;
1343*67e74705SXin Li }
1344*67e74705SXin Li
1345*67e74705SXin Li // Fake arguments aren't part of the parsed form and should not be
1346*67e74705SXin Li // pretty-printed.
1347*67e74705SXin Li bool hasNonFakeArgs = false;
1348*67e74705SXin Li for (const auto &arg : Args) {
1349*67e74705SXin Li if (arg->isFake()) continue;
1350*67e74705SXin Li hasNonFakeArgs = true;
1351*67e74705SXin Li }
1352*67e74705SXin Li
1353*67e74705SXin Li // FIXME: always printing the parenthesis isn't the correct behavior for
1354*67e74705SXin Li // attributes which have optional arguments that were not provided. For
1355*67e74705SXin Li // instance: __attribute__((aligned)) will be pretty printed as
1356*67e74705SXin Li // __attribute__((aligned())). The logic should check whether there is only
1357*67e74705SXin Li // a single argument, and if it is optional, whether it has been provided.
1358*67e74705SXin Li if (hasNonFakeArgs)
1359*67e74705SXin Li OS << "(";
1360*67e74705SXin Li if (Spelling == "availability") {
1361*67e74705SXin Li writeAvailabilityValue(OS);
1362*67e74705SXin Li } else if (Spelling == "deprecated" || Spelling == "gnu::deprecated") {
1363*67e74705SXin Li writeDeprecatedAttrValue(OS, Variety);
1364*67e74705SXin Li } else {
1365*67e74705SXin Li unsigned index = 0;
1366*67e74705SXin Li for (const auto &arg : Args) {
1367*67e74705SXin Li if (arg->isFake()) continue;
1368*67e74705SXin Li if (index++) OS << ", ";
1369*67e74705SXin Li arg->writeValue(OS);
1370*67e74705SXin Li }
1371*67e74705SXin Li }
1372*67e74705SXin Li
1373*67e74705SXin Li if (hasNonFakeArgs)
1374*67e74705SXin Li OS << ")";
1375*67e74705SXin Li OS << Suffix + "\";\n";
1376*67e74705SXin Li
1377*67e74705SXin Li OS <<
1378*67e74705SXin Li " break;\n"
1379*67e74705SXin Li " }\n";
1380*67e74705SXin Li }
1381*67e74705SXin Li
1382*67e74705SXin Li // End of the switch statement.
1383*67e74705SXin Li OS << "}\n";
1384*67e74705SXin Li // End of the print function.
1385*67e74705SXin Li OS << "}\n\n";
1386*67e74705SXin Li }
1387*67e74705SXin Li
1388*67e74705SXin Li /// \brief Return the index of a spelling in a spelling list.
1389*67e74705SXin Li static unsigned
getSpellingListIndex(const std::vector<FlattenedSpelling> & SpellingList,const FlattenedSpelling & Spelling)1390*67e74705SXin Li getSpellingListIndex(const std::vector<FlattenedSpelling> &SpellingList,
1391*67e74705SXin Li const FlattenedSpelling &Spelling) {
1392*67e74705SXin Li assert(!SpellingList.empty() && "Spelling list is empty!");
1393*67e74705SXin Li
1394*67e74705SXin Li for (unsigned Index = 0; Index < SpellingList.size(); ++Index) {
1395*67e74705SXin Li const FlattenedSpelling &S = SpellingList[Index];
1396*67e74705SXin Li if (S.variety() != Spelling.variety())
1397*67e74705SXin Li continue;
1398*67e74705SXin Li if (S.nameSpace() != Spelling.nameSpace())
1399*67e74705SXin Li continue;
1400*67e74705SXin Li if (S.name() != Spelling.name())
1401*67e74705SXin Li continue;
1402*67e74705SXin Li
1403*67e74705SXin Li return Index;
1404*67e74705SXin Li }
1405*67e74705SXin Li
1406*67e74705SXin Li llvm_unreachable("Unknown spelling!");
1407*67e74705SXin Li }
1408*67e74705SXin Li
writeAttrAccessorDefinition(const Record & R,raw_ostream & OS)1409*67e74705SXin Li static void writeAttrAccessorDefinition(const Record &R, raw_ostream &OS) {
1410*67e74705SXin Li std::vector<Record*> Accessors = R.getValueAsListOfDefs("Accessors");
1411*67e74705SXin Li for (const auto *Accessor : Accessors) {
1412*67e74705SXin Li std::string Name = Accessor->getValueAsString("Name");
1413*67e74705SXin Li std::vector<FlattenedSpelling> Spellings =
1414*67e74705SXin Li GetFlattenedSpellings(*Accessor);
1415*67e74705SXin Li std::vector<FlattenedSpelling> SpellingList = GetFlattenedSpellings(R);
1416*67e74705SXin Li assert(!SpellingList.empty() &&
1417*67e74705SXin Li "Attribute with empty spelling list can't have accessors!");
1418*67e74705SXin Li
1419*67e74705SXin Li OS << " bool " << Name << "() const { return SpellingListIndex == ";
1420*67e74705SXin Li for (unsigned Index = 0; Index < Spellings.size(); ++Index) {
1421*67e74705SXin Li OS << getSpellingListIndex(SpellingList, Spellings[Index]);
1422*67e74705SXin Li if (Index != Spellings.size() -1)
1423*67e74705SXin Li OS << " ||\n SpellingListIndex == ";
1424*67e74705SXin Li else
1425*67e74705SXin Li OS << "; }\n";
1426*67e74705SXin Li }
1427*67e74705SXin Li }
1428*67e74705SXin Li }
1429*67e74705SXin Li
1430*67e74705SXin Li static bool
SpellingNamesAreCommon(const std::vector<FlattenedSpelling> & Spellings)1431*67e74705SXin Li SpellingNamesAreCommon(const std::vector<FlattenedSpelling>& Spellings) {
1432*67e74705SXin Li assert(!Spellings.empty() && "An empty list of spellings was provided");
1433*67e74705SXin Li std::string FirstName = NormalizeNameForSpellingComparison(
1434*67e74705SXin Li Spellings.front().name());
1435*67e74705SXin Li for (const auto &Spelling :
1436*67e74705SXin Li llvm::make_range(std::next(Spellings.begin()), Spellings.end())) {
1437*67e74705SXin Li std::string Name = NormalizeNameForSpellingComparison(Spelling.name());
1438*67e74705SXin Li if (Name != FirstName)
1439*67e74705SXin Li return false;
1440*67e74705SXin Li }
1441*67e74705SXin Li return true;
1442*67e74705SXin Li }
1443*67e74705SXin Li
1444*67e74705SXin Li typedef std::map<unsigned, std::string> SemanticSpellingMap;
1445*67e74705SXin Li static std::string
CreateSemanticSpellings(const std::vector<FlattenedSpelling> & Spellings,SemanticSpellingMap & Map)1446*67e74705SXin Li CreateSemanticSpellings(const std::vector<FlattenedSpelling> &Spellings,
1447*67e74705SXin Li SemanticSpellingMap &Map) {
1448*67e74705SXin Li // The enumerants are automatically generated based on the variety,
1449*67e74705SXin Li // namespace (if present) and name for each attribute spelling. However,
1450*67e74705SXin Li // care is taken to avoid trampling on the reserved namespace due to
1451*67e74705SXin Li // underscores.
1452*67e74705SXin Li std::string Ret(" enum Spelling {\n");
1453*67e74705SXin Li std::set<std::string> Uniques;
1454*67e74705SXin Li unsigned Idx = 0;
1455*67e74705SXin Li for (auto I = Spellings.begin(), E = Spellings.end(); I != E; ++I, ++Idx) {
1456*67e74705SXin Li const FlattenedSpelling &S = *I;
1457*67e74705SXin Li const std::string &Variety = S.variety();
1458*67e74705SXin Li const std::string &Spelling = S.name();
1459*67e74705SXin Li const std::string &Namespace = S.nameSpace();
1460*67e74705SXin Li std::string EnumName;
1461*67e74705SXin Li
1462*67e74705SXin Li EnumName += (Variety + "_");
1463*67e74705SXin Li if (!Namespace.empty())
1464*67e74705SXin Li EnumName += (NormalizeNameForSpellingComparison(Namespace).str() +
1465*67e74705SXin Li "_");
1466*67e74705SXin Li EnumName += NormalizeNameForSpellingComparison(Spelling);
1467*67e74705SXin Li
1468*67e74705SXin Li // Even if the name is not unique, this spelling index corresponds to a
1469*67e74705SXin Li // particular enumerant name that we've calculated.
1470*67e74705SXin Li Map[Idx] = EnumName;
1471*67e74705SXin Li
1472*67e74705SXin Li // Since we have been stripping underscores to avoid trampling on the
1473*67e74705SXin Li // reserved namespace, we may have inadvertently created duplicate
1474*67e74705SXin Li // enumerant names. These duplicates are not considered part of the
1475*67e74705SXin Li // semantic spelling, and can be elided.
1476*67e74705SXin Li if (Uniques.find(EnumName) != Uniques.end())
1477*67e74705SXin Li continue;
1478*67e74705SXin Li
1479*67e74705SXin Li Uniques.insert(EnumName);
1480*67e74705SXin Li if (I != Spellings.begin())
1481*67e74705SXin Li Ret += ",\n";
1482*67e74705SXin Li // Duplicate spellings are not considered part of the semantic spelling
1483*67e74705SXin Li // enumeration, but the spelling index and semantic spelling values are
1484*67e74705SXin Li // meant to be equivalent, so we must specify a concrete value for each
1485*67e74705SXin Li // enumerator.
1486*67e74705SXin Li Ret += " " + EnumName + " = " + llvm::utostr(Idx);
1487*67e74705SXin Li }
1488*67e74705SXin Li Ret += "\n };\n\n";
1489*67e74705SXin Li return Ret;
1490*67e74705SXin Li }
1491*67e74705SXin Li
WriteSemanticSpellingSwitch(const std::string & VarName,const SemanticSpellingMap & Map,raw_ostream & OS)1492*67e74705SXin Li void WriteSemanticSpellingSwitch(const std::string &VarName,
1493*67e74705SXin Li const SemanticSpellingMap &Map,
1494*67e74705SXin Li raw_ostream &OS) {
1495*67e74705SXin Li OS << " switch (" << VarName << ") {\n default: "
1496*67e74705SXin Li << "llvm_unreachable(\"Unknown spelling list index\");\n";
1497*67e74705SXin Li for (const auto &I : Map)
1498*67e74705SXin Li OS << " case " << I.first << ": return " << I.second << ";\n";
1499*67e74705SXin Li OS << " }\n";
1500*67e74705SXin Li }
1501*67e74705SXin Li
1502*67e74705SXin Li // Emits the LateParsed property for attributes.
emitClangAttrLateParsedList(RecordKeeper & Records,raw_ostream & OS)1503*67e74705SXin Li static void emitClangAttrLateParsedList(RecordKeeper &Records, raw_ostream &OS) {
1504*67e74705SXin Li OS << "#if defined(CLANG_ATTR_LATE_PARSED_LIST)\n";
1505*67e74705SXin Li std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1506*67e74705SXin Li
1507*67e74705SXin Li for (const auto *Attr : Attrs) {
1508*67e74705SXin Li bool LateParsed = Attr->getValueAsBit("LateParsed");
1509*67e74705SXin Li
1510*67e74705SXin Li if (LateParsed) {
1511*67e74705SXin Li std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
1512*67e74705SXin Li
1513*67e74705SXin Li // FIXME: Handle non-GNU attributes
1514*67e74705SXin Li for (const auto &I : Spellings) {
1515*67e74705SXin Li if (I.variety() != "GNU")
1516*67e74705SXin Li continue;
1517*67e74705SXin Li OS << ".Case(\"" << I.name() << "\", " << LateParsed << ")\n";
1518*67e74705SXin Li }
1519*67e74705SXin Li }
1520*67e74705SXin Li }
1521*67e74705SXin Li OS << "#endif // CLANG_ATTR_LATE_PARSED_LIST\n\n";
1522*67e74705SXin Li }
1523*67e74705SXin Li
1524*67e74705SXin Li /// \brief Emits the first-argument-is-type property for attributes.
emitClangAttrTypeArgList(RecordKeeper & Records,raw_ostream & OS)1525*67e74705SXin Li static void emitClangAttrTypeArgList(RecordKeeper &Records, raw_ostream &OS) {
1526*67e74705SXin Li OS << "#if defined(CLANG_ATTR_TYPE_ARG_LIST)\n";
1527*67e74705SXin Li std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
1528*67e74705SXin Li
1529*67e74705SXin Li for (const auto *Attr : Attrs) {
1530*67e74705SXin Li // Determine whether the first argument is a type.
1531*67e74705SXin Li std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args");
1532*67e74705SXin Li if (Args.empty())
1533*67e74705SXin Li continue;
1534*67e74705SXin Li
1535*67e74705SXin Li if (Args[0]->getSuperClasses().back().first->getName() != "TypeArgument")
1536*67e74705SXin Li continue;
1537*67e74705SXin Li
1538*67e74705SXin Li // All these spellings take a single type argument.
1539*67e74705SXin Li std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
1540*67e74705SXin Li std::set<std::string> Emitted;
1541*67e74705SXin Li for (const auto &S : Spellings) {
1542*67e74705SXin Li if (Emitted.insert(S.name()).second)
1543*67e74705SXin Li OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
1544*67e74705SXin Li }
1545*67e74705SXin Li }
1546*67e74705SXin Li OS << "#endif // CLANG_ATTR_TYPE_ARG_LIST\n\n";
1547*67e74705SXin Li }
1548*67e74705SXin Li
1549*67e74705SXin Li /// \brief Emits the parse-arguments-in-unevaluated-context property for
1550*67e74705SXin Li /// attributes.
emitClangAttrArgContextList(RecordKeeper & Records,raw_ostream & OS)1551*67e74705SXin Li static void emitClangAttrArgContextList(RecordKeeper &Records, raw_ostream &OS) {
1552*67e74705SXin Li OS << "#if defined(CLANG_ATTR_ARG_CONTEXT_LIST)\n";
1553*67e74705SXin Li ParsedAttrMap Attrs = getParsedAttrList(Records);
1554*67e74705SXin Li for (const auto &I : Attrs) {
1555*67e74705SXin Li const Record &Attr = *I.second;
1556*67e74705SXin Li
1557*67e74705SXin Li if (!Attr.getValueAsBit("ParseArgumentsAsUnevaluated"))
1558*67e74705SXin Li continue;
1559*67e74705SXin Li
1560*67e74705SXin Li // All these spellings take are parsed unevaluated.
1561*67e74705SXin Li std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
1562*67e74705SXin Li std::set<std::string> Emitted;
1563*67e74705SXin Li for (const auto &S : Spellings) {
1564*67e74705SXin Li if (Emitted.insert(S.name()).second)
1565*67e74705SXin Li OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
1566*67e74705SXin Li }
1567*67e74705SXin Li }
1568*67e74705SXin Li OS << "#endif // CLANG_ATTR_ARG_CONTEXT_LIST\n\n";
1569*67e74705SXin Li }
1570*67e74705SXin Li
isIdentifierArgument(Record * Arg)1571*67e74705SXin Li static bool isIdentifierArgument(Record *Arg) {
1572*67e74705SXin Li return !Arg->getSuperClasses().empty() &&
1573*67e74705SXin Li llvm::StringSwitch<bool>(Arg->getSuperClasses().back().first->getName())
1574*67e74705SXin Li .Case("IdentifierArgument", true)
1575*67e74705SXin Li .Case("EnumArgument", true)
1576*67e74705SXin Li .Case("VariadicEnumArgument", true)
1577*67e74705SXin Li .Default(false);
1578*67e74705SXin Li }
1579*67e74705SXin Li
1580*67e74705SXin Li // Emits the first-argument-is-identifier property for attributes.
emitClangAttrIdentifierArgList(RecordKeeper & Records,raw_ostream & OS)1581*67e74705SXin Li static void emitClangAttrIdentifierArgList(RecordKeeper &Records, raw_ostream &OS) {
1582*67e74705SXin Li OS << "#if defined(CLANG_ATTR_IDENTIFIER_ARG_LIST)\n";
1583*67e74705SXin Li std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1584*67e74705SXin Li
1585*67e74705SXin Li for (const auto *Attr : Attrs) {
1586*67e74705SXin Li // Determine whether the first argument is an identifier.
1587*67e74705SXin Li std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args");
1588*67e74705SXin Li if (Args.empty() || !isIdentifierArgument(Args[0]))
1589*67e74705SXin Li continue;
1590*67e74705SXin Li
1591*67e74705SXin Li // All these spellings take an identifier argument.
1592*67e74705SXin Li std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
1593*67e74705SXin Li std::set<std::string> Emitted;
1594*67e74705SXin Li for (const auto &S : Spellings) {
1595*67e74705SXin Li if (Emitted.insert(S.name()).second)
1596*67e74705SXin Li OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
1597*67e74705SXin Li }
1598*67e74705SXin Li }
1599*67e74705SXin Li OS << "#endif // CLANG_ATTR_IDENTIFIER_ARG_LIST\n\n";
1600*67e74705SXin Li }
1601*67e74705SXin Li
1602*67e74705SXin Li namespace clang {
1603*67e74705SXin Li
1604*67e74705SXin Li // Emits the class definitions for attributes.
EmitClangAttrClass(RecordKeeper & Records,raw_ostream & OS)1605*67e74705SXin Li void EmitClangAttrClass(RecordKeeper &Records, raw_ostream &OS) {
1606*67e74705SXin Li emitSourceFileHeader("Attribute classes' definitions", OS);
1607*67e74705SXin Li
1608*67e74705SXin Li OS << "#ifndef LLVM_CLANG_ATTR_CLASSES_INC\n";
1609*67e74705SXin Li OS << "#define LLVM_CLANG_ATTR_CLASSES_INC\n\n";
1610*67e74705SXin Li
1611*67e74705SXin Li std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1612*67e74705SXin Li
1613*67e74705SXin Li for (const auto *Attr : Attrs) {
1614*67e74705SXin Li const Record &R = *Attr;
1615*67e74705SXin Li
1616*67e74705SXin Li // FIXME: Currently, documentation is generated as-needed due to the fact
1617*67e74705SXin Li // that there is no way to allow a generated project "reach into" the docs
1618*67e74705SXin Li // directory (for instance, it may be an out-of-tree build). However, we want
1619*67e74705SXin Li // to ensure that every attribute has a Documentation field, and produce an
1620*67e74705SXin Li // error if it has been neglected. Otherwise, the on-demand generation which
1621*67e74705SXin Li // happens server-side will fail. This code is ensuring that functionality,
1622*67e74705SXin Li // even though this Emitter doesn't technically need the documentation.
1623*67e74705SXin Li // When attribute documentation can be generated as part of the build
1624*67e74705SXin Li // itself, this code can be removed.
1625*67e74705SXin Li (void)R.getValueAsListOfDefs("Documentation");
1626*67e74705SXin Li
1627*67e74705SXin Li if (!R.getValueAsBit("ASTNode"))
1628*67e74705SXin Li continue;
1629*67e74705SXin Li
1630*67e74705SXin Li ArrayRef<std::pair<Record *, SMRange>> Supers = R.getSuperClasses();
1631*67e74705SXin Li assert(!Supers.empty() && "Forgot to specify a superclass for the attr");
1632*67e74705SXin Li std::string SuperName;
1633*67e74705SXin Li for (const auto &Super : llvm::reverse(Supers)) {
1634*67e74705SXin Li const Record *R = Super.first;
1635*67e74705SXin Li if (R->getName() != "TargetSpecificAttr" && SuperName.empty())
1636*67e74705SXin Li SuperName = R->getName();
1637*67e74705SXin Li }
1638*67e74705SXin Li
1639*67e74705SXin Li OS << "class " << R.getName() << "Attr : public " << SuperName << " {\n";
1640*67e74705SXin Li
1641*67e74705SXin Li std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
1642*67e74705SXin Li std::vector<std::unique_ptr<Argument>> Args;
1643*67e74705SXin Li Args.reserve(ArgRecords.size());
1644*67e74705SXin Li
1645*67e74705SXin Li bool HasOptArg = false;
1646*67e74705SXin Li bool HasFakeArg = false;
1647*67e74705SXin Li for (const auto *ArgRecord : ArgRecords) {
1648*67e74705SXin Li Args.emplace_back(createArgument(*ArgRecord, R.getName()));
1649*67e74705SXin Li Args.back()->writeDeclarations(OS);
1650*67e74705SXin Li OS << "\n\n";
1651*67e74705SXin Li
1652*67e74705SXin Li // For these purposes, fake takes priority over optional.
1653*67e74705SXin Li if (Args.back()->isFake()) {
1654*67e74705SXin Li HasFakeArg = true;
1655*67e74705SXin Li } else if (Args.back()->isOptional()) {
1656*67e74705SXin Li HasOptArg = true;
1657*67e74705SXin Li }
1658*67e74705SXin Li }
1659*67e74705SXin Li
1660*67e74705SXin Li OS << "public:\n";
1661*67e74705SXin Li
1662*67e74705SXin Li std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
1663*67e74705SXin Li
1664*67e74705SXin Li // If there are zero or one spellings, all spelling-related functionality
1665*67e74705SXin Li // can be elided. If all of the spellings share the same name, the spelling
1666*67e74705SXin Li // functionality can also be elided.
1667*67e74705SXin Li bool ElideSpelling = (Spellings.size() <= 1) ||
1668*67e74705SXin Li SpellingNamesAreCommon(Spellings);
1669*67e74705SXin Li
1670*67e74705SXin Li // This maps spelling index values to semantic Spelling enumerants.
1671*67e74705SXin Li SemanticSpellingMap SemanticToSyntacticMap;
1672*67e74705SXin Li
1673*67e74705SXin Li if (!ElideSpelling)
1674*67e74705SXin Li OS << CreateSemanticSpellings(Spellings, SemanticToSyntacticMap);
1675*67e74705SXin Li
1676*67e74705SXin Li // Emit CreateImplicit factory methods.
1677*67e74705SXin Li auto emitCreateImplicit = [&](bool emitFake) {
1678*67e74705SXin Li OS << " static " << R.getName() << "Attr *CreateImplicit(";
1679*67e74705SXin Li OS << "ASTContext &Ctx";
1680*67e74705SXin Li if (!ElideSpelling)
1681*67e74705SXin Li OS << ", Spelling S";
1682*67e74705SXin Li for (auto const &ai : Args) {
1683*67e74705SXin Li if (ai->isFake() && !emitFake) continue;
1684*67e74705SXin Li OS << ", ";
1685*67e74705SXin Li ai->writeCtorParameters(OS);
1686*67e74705SXin Li }
1687*67e74705SXin Li OS << ", SourceRange Loc = SourceRange()";
1688*67e74705SXin Li OS << ") {\n";
1689*67e74705SXin Li OS << " auto *A = new (Ctx) " << R.getName();
1690*67e74705SXin Li OS << "Attr(Loc, Ctx, ";
1691*67e74705SXin Li for (auto const &ai : Args) {
1692*67e74705SXin Li if (ai->isFake() && !emitFake) continue;
1693*67e74705SXin Li ai->writeImplicitCtorArgs(OS);
1694*67e74705SXin Li OS << ", ";
1695*67e74705SXin Li }
1696*67e74705SXin Li OS << (ElideSpelling ? "0" : "S") << ");\n";
1697*67e74705SXin Li OS << " A->setImplicit(true);\n";
1698*67e74705SXin Li OS << " return A;\n }\n\n";
1699*67e74705SXin Li };
1700*67e74705SXin Li
1701*67e74705SXin Li // Emit a CreateImplicit that takes all the arguments.
1702*67e74705SXin Li emitCreateImplicit(true);
1703*67e74705SXin Li
1704*67e74705SXin Li // Emit a CreateImplicit that takes all the non-fake arguments.
1705*67e74705SXin Li if (HasFakeArg) {
1706*67e74705SXin Li emitCreateImplicit(false);
1707*67e74705SXin Li }
1708*67e74705SXin Li
1709*67e74705SXin Li // Emit constructors.
1710*67e74705SXin Li auto emitCtor = [&](bool emitOpt, bool emitFake) {
1711*67e74705SXin Li auto shouldEmitArg = [=](const std::unique_ptr<Argument> &arg) {
1712*67e74705SXin Li if (arg->isFake()) return emitFake;
1713*67e74705SXin Li if (arg->isOptional()) return emitOpt;
1714*67e74705SXin Li return true;
1715*67e74705SXin Li };
1716*67e74705SXin Li
1717*67e74705SXin Li OS << " " << R.getName() << "Attr(SourceRange R, ASTContext &Ctx\n";
1718*67e74705SXin Li for (auto const &ai : Args) {
1719*67e74705SXin Li if (!shouldEmitArg(ai)) continue;
1720*67e74705SXin Li OS << " , ";
1721*67e74705SXin Li ai->writeCtorParameters(OS);
1722*67e74705SXin Li OS << "\n";
1723*67e74705SXin Li }
1724*67e74705SXin Li
1725*67e74705SXin Li OS << " , ";
1726*67e74705SXin Li OS << "unsigned SI\n";
1727*67e74705SXin Li
1728*67e74705SXin Li OS << " )\n";
1729*67e74705SXin Li OS << " : " << SuperName << "(attr::" << R.getName() << ", R, SI, "
1730*67e74705SXin Li << ( R.getValueAsBit("LateParsed") ? "true" : "false" ) << ", "
1731*67e74705SXin Li << ( R.getValueAsBit("DuplicatesAllowedWhileMerging") ? "true" : "false" ) << ")\n";
1732*67e74705SXin Li
1733*67e74705SXin Li for (auto const &ai : Args) {
1734*67e74705SXin Li OS << " , ";
1735*67e74705SXin Li if (!shouldEmitArg(ai)) {
1736*67e74705SXin Li ai->writeCtorDefaultInitializers(OS);
1737*67e74705SXin Li } else {
1738*67e74705SXin Li ai->writeCtorInitializers(OS);
1739*67e74705SXin Li }
1740*67e74705SXin Li OS << "\n";
1741*67e74705SXin Li }
1742*67e74705SXin Li
1743*67e74705SXin Li OS << " {\n";
1744*67e74705SXin Li
1745*67e74705SXin Li for (auto const &ai : Args) {
1746*67e74705SXin Li if (!shouldEmitArg(ai)) continue;
1747*67e74705SXin Li ai->writeCtorBody(OS);
1748*67e74705SXin Li }
1749*67e74705SXin Li OS << " }\n\n";
1750*67e74705SXin Li };
1751*67e74705SXin Li
1752*67e74705SXin Li // Emit a constructor that includes all the arguments.
1753*67e74705SXin Li // This is necessary for cloning.
1754*67e74705SXin Li emitCtor(true, true);
1755*67e74705SXin Li
1756*67e74705SXin Li // Emit a constructor that takes all the non-fake arguments.
1757*67e74705SXin Li if (HasFakeArg) {
1758*67e74705SXin Li emitCtor(true, false);
1759*67e74705SXin Li }
1760*67e74705SXin Li
1761*67e74705SXin Li // Emit a constructor that takes all the non-fake, non-optional arguments.
1762*67e74705SXin Li if (HasOptArg) {
1763*67e74705SXin Li emitCtor(false, false);
1764*67e74705SXin Li }
1765*67e74705SXin Li
1766*67e74705SXin Li OS << " " << R.getName() << "Attr *clone(ASTContext &C) const;\n";
1767*67e74705SXin Li OS << " void printPretty(raw_ostream &OS,\n"
1768*67e74705SXin Li << " const PrintingPolicy &Policy) const;\n";
1769*67e74705SXin Li OS << " const char *getSpelling() const;\n";
1770*67e74705SXin Li
1771*67e74705SXin Li if (!ElideSpelling) {
1772*67e74705SXin Li assert(!SemanticToSyntacticMap.empty() && "Empty semantic mapping list");
1773*67e74705SXin Li OS << " Spelling getSemanticSpelling() const {\n";
1774*67e74705SXin Li WriteSemanticSpellingSwitch("SpellingListIndex", SemanticToSyntacticMap,
1775*67e74705SXin Li OS);
1776*67e74705SXin Li OS << " }\n";
1777*67e74705SXin Li }
1778*67e74705SXin Li
1779*67e74705SXin Li writeAttrAccessorDefinition(R, OS);
1780*67e74705SXin Li
1781*67e74705SXin Li for (auto const &ai : Args) {
1782*67e74705SXin Li ai->writeAccessors(OS);
1783*67e74705SXin Li OS << "\n\n";
1784*67e74705SXin Li
1785*67e74705SXin Li // Don't write conversion routines for fake arguments.
1786*67e74705SXin Li if (ai->isFake()) continue;
1787*67e74705SXin Li
1788*67e74705SXin Li if (ai->isEnumArg())
1789*67e74705SXin Li static_cast<const EnumArgument *>(ai.get())->writeConversion(OS);
1790*67e74705SXin Li else if (ai->isVariadicEnumArg())
1791*67e74705SXin Li static_cast<const VariadicEnumArgument *>(ai.get())
1792*67e74705SXin Li ->writeConversion(OS);
1793*67e74705SXin Li }
1794*67e74705SXin Li
1795*67e74705SXin Li OS << R.getValueAsString("AdditionalMembers");
1796*67e74705SXin Li OS << "\n\n";
1797*67e74705SXin Li
1798*67e74705SXin Li OS << " static bool classof(const Attr *A) { return A->getKind() == "
1799*67e74705SXin Li << "attr::" << R.getName() << "; }\n";
1800*67e74705SXin Li
1801*67e74705SXin Li OS << "};\n\n";
1802*67e74705SXin Li }
1803*67e74705SXin Li
1804*67e74705SXin Li OS << "#endif // LLVM_CLANG_ATTR_CLASSES_INC\n";
1805*67e74705SXin Li }
1806*67e74705SXin Li
1807*67e74705SXin Li // Emits the class method definitions for attributes.
EmitClangAttrImpl(RecordKeeper & Records,raw_ostream & OS)1808*67e74705SXin Li void EmitClangAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
1809*67e74705SXin Li emitSourceFileHeader("Attribute classes' member function definitions", OS);
1810*67e74705SXin Li
1811*67e74705SXin Li std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1812*67e74705SXin Li
1813*67e74705SXin Li for (auto *Attr : Attrs) {
1814*67e74705SXin Li Record &R = *Attr;
1815*67e74705SXin Li
1816*67e74705SXin Li if (!R.getValueAsBit("ASTNode"))
1817*67e74705SXin Li continue;
1818*67e74705SXin Li
1819*67e74705SXin Li std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
1820*67e74705SXin Li std::vector<std::unique_ptr<Argument>> Args;
1821*67e74705SXin Li for (const auto *Arg : ArgRecords)
1822*67e74705SXin Li Args.emplace_back(createArgument(*Arg, R.getName()));
1823*67e74705SXin Li
1824*67e74705SXin Li for (auto const &ai : Args)
1825*67e74705SXin Li ai->writeAccessorDefinitions(OS);
1826*67e74705SXin Li
1827*67e74705SXin Li OS << R.getName() << "Attr *" << R.getName()
1828*67e74705SXin Li << "Attr::clone(ASTContext &C) const {\n";
1829*67e74705SXin Li OS << " auto *A = new (C) " << R.getName() << "Attr(getLocation(), C";
1830*67e74705SXin Li for (auto const &ai : Args) {
1831*67e74705SXin Li OS << ", ";
1832*67e74705SXin Li ai->writeCloneArgs(OS);
1833*67e74705SXin Li }
1834*67e74705SXin Li OS << ", getSpellingListIndex());\n";
1835*67e74705SXin Li OS << " A->Inherited = Inherited;\n";
1836*67e74705SXin Li OS << " A->IsPackExpansion = IsPackExpansion;\n";
1837*67e74705SXin Li OS << " A->Implicit = Implicit;\n";
1838*67e74705SXin Li OS << " return A;\n}\n\n";
1839*67e74705SXin Li
1840*67e74705SXin Li writePrettyPrintFunction(R, Args, OS);
1841*67e74705SXin Li writeGetSpellingFunction(R, OS);
1842*67e74705SXin Li }
1843*67e74705SXin Li
1844*67e74705SXin Li // Instead of relying on virtual dispatch we just create a huge dispatch
1845*67e74705SXin Li // switch. This is both smaller and faster than virtual functions.
1846*67e74705SXin Li auto EmitFunc = [&](const char *Method) {
1847*67e74705SXin Li OS << " switch (getKind()) {\n";
1848*67e74705SXin Li for (const auto *Attr : Attrs) {
1849*67e74705SXin Li const Record &R = *Attr;
1850*67e74705SXin Li if (!R.getValueAsBit("ASTNode"))
1851*67e74705SXin Li continue;
1852*67e74705SXin Li
1853*67e74705SXin Li OS << " case attr::" << R.getName() << ":\n";
1854*67e74705SXin Li OS << " return cast<" << R.getName() << "Attr>(this)->" << Method
1855*67e74705SXin Li << ";\n";
1856*67e74705SXin Li }
1857*67e74705SXin Li OS << " }\n";
1858*67e74705SXin Li OS << " llvm_unreachable(\"Unexpected attribute kind!\");\n";
1859*67e74705SXin Li OS << "}\n\n";
1860*67e74705SXin Li };
1861*67e74705SXin Li
1862*67e74705SXin Li OS << "const char *Attr::getSpelling() const {\n";
1863*67e74705SXin Li EmitFunc("getSpelling()");
1864*67e74705SXin Li
1865*67e74705SXin Li OS << "Attr *Attr::clone(ASTContext &C) const {\n";
1866*67e74705SXin Li EmitFunc("clone(C)");
1867*67e74705SXin Li
1868*67e74705SXin Li OS << "void Attr::printPretty(raw_ostream &OS, "
1869*67e74705SXin Li "const PrintingPolicy &Policy) const {\n";
1870*67e74705SXin Li EmitFunc("printPretty(OS, Policy)");
1871*67e74705SXin Li }
1872*67e74705SXin Li
1873*67e74705SXin Li } // end namespace clang
1874*67e74705SXin Li
emitAttrList(raw_ostream & OS,StringRef Class,const std::vector<Record * > & AttrList)1875*67e74705SXin Li static void emitAttrList(raw_ostream &OS, StringRef Class,
1876*67e74705SXin Li const std::vector<Record*> &AttrList) {
1877*67e74705SXin Li for (auto Cur : AttrList) {
1878*67e74705SXin Li OS << Class << "(" << Cur->getName() << ")\n";
1879*67e74705SXin Li }
1880*67e74705SXin Li }
1881*67e74705SXin Li
1882*67e74705SXin Li // Determines if an attribute has a Pragma spelling.
AttrHasPragmaSpelling(const Record * R)1883*67e74705SXin Li static bool AttrHasPragmaSpelling(const Record *R) {
1884*67e74705SXin Li std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R);
1885*67e74705SXin Li return std::find_if(Spellings.begin(), Spellings.end(),
1886*67e74705SXin Li [](const FlattenedSpelling &S) {
1887*67e74705SXin Li return S.variety() == "Pragma";
1888*67e74705SXin Li }) != Spellings.end();
1889*67e74705SXin Li }
1890*67e74705SXin Li
1891*67e74705SXin Li namespace {
1892*67e74705SXin Li
1893*67e74705SXin Li struct AttrClassDescriptor {
1894*67e74705SXin Li const char * const MacroName;
1895*67e74705SXin Li const char * const TableGenName;
1896*67e74705SXin Li };
1897*67e74705SXin Li
1898*67e74705SXin Li } // end anonymous namespace
1899*67e74705SXin Li
1900*67e74705SXin Li static const AttrClassDescriptor AttrClassDescriptors[] = {
1901*67e74705SXin Li { "ATTR", "Attr" },
1902*67e74705SXin Li { "STMT_ATTR", "StmtAttr" },
1903*67e74705SXin Li { "INHERITABLE_ATTR", "InheritableAttr" },
1904*67e74705SXin Li { "INHERITABLE_PARAM_ATTR", "InheritableParamAttr" },
1905*67e74705SXin Li { "PARAMETER_ABI_ATTR", "ParameterABIAttr" }
1906*67e74705SXin Li };
1907*67e74705SXin Li
emitDefaultDefine(raw_ostream & OS,StringRef name,const char * superName)1908*67e74705SXin Li static void emitDefaultDefine(raw_ostream &OS, StringRef name,
1909*67e74705SXin Li const char *superName) {
1910*67e74705SXin Li OS << "#ifndef " << name << "\n";
1911*67e74705SXin Li OS << "#define " << name << "(NAME) ";
1912*67e74705SXin Li if (superName) OS << superName << "(NAME)";
1913*67e74705SXin Li OS << "\n#endif\n\n";
1914*67e74705SXin Li }
1915*67e74705SXin Li
1916*67e74705SXin Li namespace {
1917*67e74705SXin Li
1918*67e74705SXin Li /// A class of attributes.
1919*67e74705SXin Li struct AttrClass {
1920*67e74705SXin Li const AttrClassDescriptor &Descriptor;
1921*67e74705SXin Li Record *TheRecord;
1922*67e74705SXin Li AttrClass *SuperClass = nullptr;
1923*67e74705SXin Li std::vector<AttrClass*> SubClasses;
1924*67e74705SXin Li std::vector<Record*> Attrs;
1925*67e74705SXin Li
AttrClass__anonff89b1970911::AttrClass1926*67e74705SXin Li AttrClass(const AttrClassDescriptor &Descriptor, Record *R)
1927*67e74705SXin Li : Descriptor(Descriptor), TheRecord(R) {}
1928*67e74705SXin Li
emitDefaultDefines__anonff89b1970911::AttrClass1929*67e74705SXin Li void emitDefaultDefines(raw_ostream &OS) const {
1930*67e74705SXin Li // Default the macro unless this is a root class (i.e. Attr).
1931*67e74705SXin Li if (SuperClass) {
1932*67e74705SXin Li emitDefaultDefine(OS, Descriptor.MacroName,
1933*67e74705SXin Li SuperClass->Descriptor.MacroName);
1934*67e74705SXin Li }
1935*67e74705SXin Li }
1936*67e74705SXin Li
emitUndefs__anonff89b1970911::AttrClass1937*67e74705SXin Li void emitUndefs(raw_ostream &OS) const {
1938*67e74705SXin Li OS << "#undef " << Descriptor.MacroName << "\n";
1939*67e74705SXin Li }
1940*67e74705SXin Li
emitAttrList__anonff89b1970911::AttrClass1941*67e74705SXin Li void emitAttrList(raw_ostream &OS) const {
1942*67e74705SXin Li for (auto SubClass : SubClasses) {
1943*67e74705SXin Li SubClass->emitAttrList(OS);
1944*67e74705SXin Li }
1945*67e74705SXin Li
1946*67e74705SXin Li ::emitAttrList(OS, Descriptor.MacroName, Attrs);
1947*67e74705SXin Li }
1948*67e74705SXin Li
classifyAttrOnRoot__anonff89b1970911::AttrClass1949*67e74705SXin Li void classifyAttrOnRoot(Record *Attr) {
1950*67e74705SXin Li bool result = classifyAttr(Attr);
1951*67e74705SXin Li assert(result && "failed to classify on root"); (void) result;
1952*67e74705SXin Li }
1953*67e74705SXin Li
emitAttrRange__anonff89b1970911::AttrClass1954*67e74705SXin Li void emitAttrRange(raw_ostream &OS) const {
1955*67e74705SXin Li OS << "ATTR_RANGE(" << Descriptor.TableGenName
1956*67e74705SXin Li << ", " << getFirstAttr()->getName()
1957*67e74705SXin Li << ", " << getLastAttr()->getName() << ")\n";
1958*67e74705SXin Li }
1959*67e74705SXin Li
1960*67e74705SXin Li private:
classifyAttr__anonff89b1970911::AttrClass1961*67e74705SXin Li bool classifyAttr(Record *Attr) {
1962*67e74705SXin Li // Check all the subclasses.
1963*67e74705SXin Li for (auto SubClass : SubClasses) {
1964*67e74705SXin Li if (SubClass->classifyAttr(Attr))
1965*67e74705SXin Li return true;
1966*67e74705SXin Li }
1967*67e74705SXin Li
1968*67e74705SXin Li // It's not more specific than this class, but it might still belong here.
1969*67e74705SXin Li if (Attr->isSubClassOf(TheRecord)) {
1970*67e74705SXin Li Attrs.push_back(Attr);
1971*67e74705SXin Li return true;
1972*67e74705SXin Li }
1973*67e74705SXin Li
1974*67e74705SXin Li return false;
1975*67e74705SXin Li }
1976*67e74705SXin Li
getFirstAttr__anonff89b1970911::AttrClass1977*67e74705SXin Li Record *getFirstAttr() const {
1978*67e74705SXin Li if (!SubClasses.empty())
1979*67e74705SXin Li return SubClasses.front()->getFirstAttr();
1980*67e74705SXin Li return Attrs.front();
1981*67e74705SXin Li }
1982*67e74705SXin Li
getLastAttr__anonff89b1970911::AttrClass1983*67e74705SXin Li Record *getLastAttr() const {
1984*67e74705SXin Li if (!Attrs.empty())
1985*67e74705SXin Li return Attrs.back();
1986*67e74705SXin Li return SubClasses.back()->getLastAttr();
1987*67e74705SXin Li }
1988*67e74705SXin Li };
1989*67e74705SXin Li
1990*67e74705SXin Li /// The entire hierarchy of attribute classes.
1991*67e74705SXin Li class AttrClassHierarchy {
1992*67e74705SXin Li std::vector<std::unique_ptr<AttrClass>> Classes;
1993*67e74705SXin Li
1994*67e74705SXin Li public:
AttrClassHierarchy(RecordKeeper & Records)1995*67e74705SXin Li AttrClassHierarchy(RecordKeeper &Records) {
1996*67e74705SXin Li // Find records for all the classes.
1997*67e74705SXin Li for (auto &Descriptor : AttrClassDescriptors) {
1998*67e74705SXin Li Record *ClassRecord = Records.getClass(Descriptor.TableGenName);
1999*67e74705SXin Li AttrClass *Class = new AttrClass(Descriptor, ClassRecord);
2000*67e74705SXin Li Classes.emplace_back(Class);
2001*67e74705SXin Li }
2002*67e74705SXin Li
2003*67e74705SXin Li // Link up the hierarchy.
2004*67e74705SXin Li for (auto &Class : Classes) {
2005*67e74705SXin Li if (AttrClass *SuperClass = findSuperClass(Class->TheRecord)) {
2006*67e74705SXin Li Class->SuperClass = SuperClass;
2007*67e74705SXin Li SuperClass->SubClasses.push_back(Class.get());
2008*67e74705SXin Li }
2009*67e74705SXin Li }
2010*67e74705SXin Li
2011*67e74705SXin Li #ifndef NDEBUG
2012*67e74705SXin Li for (auto i = Classes.begin(), e = Classes.end(); i != e; ++i) {
2013*67e74705SXin Li assert((i == Classes.begin()) == ((*i)->SuperClass == nullptr) &&
2014*67e74705SXin Li "only the first class should be a root class!");
2015*67e74705SXin Li }
2016*67e74705SXin Li #endif
2017*67e74705SXin Li }
2018*67e74705SXin Li
emitDefaultDefines(raw_ostream & OS) const2019*67e74705SXin Li void emitDefaultDefines(raw_ostream &OS) const {
2020*67e74705SXin Li for (auto &Class : Classes) {
2021*67e74705SXin Li Class->emitDefaultDefines(OS);
2022*67e74705SXin Li }
2023*67e74705SXin Li }
2024*67e74705SXin Li
emitUndefs(raw_ostream & OS) const2025*67e74705SXin Li void emitUndefs(raw_ostream &OS) const {
2026*67e74705SXin Li for (auto &Class : Classes) {
2027*67e74705SXin Li Class->emitUndefs(OS);
2028*67e74705SXin Li }
2029*67e74705SXin Li }
2030*67e74705SXin Li
emitAttrLists(raw_ostream & OS) const2031*67e74705SXin Li void emitAttrLists(raw_ostream &OS) const {
2032*67e74705SXin Li // Just start from the root class.
2033*67e74705SXin Li Classes[0]->emitAttrList(OS);
2034*67e74705SXin Li }
2035*67e74705SXin Li
emitAttrRanges(raw_ostream & OS) const2036*67e74705SXin Li void emitAttrRanges(raw_ostream &OS) const {
2037*67e74705SXin Li for (auto &Class : Classes)
2038*67e74705SXin Li Class->emitAttrRange(OS);
2039*67e74705SXin Li }
2040*67e74705SXin Li
classifyAttr(Record * Attr)2041*67e74705SXin Li void classifyAttr(Record *Attr) {
2042*67e74705SXin Li // Add the attribute to the root class.
2043*67e74705SXin Li Classes[0]->classifyAttrOnRoot(Attr);
2044*67e74705SXin Li }
2045*67e74705SXin Li
2046*67e74705SXin Li private:
findClassByRecord(Record * R) const2047*67e74705SXin Li AttrClass *findClassByRecord(Record *R) const {
2048*67e74705SXin Li for (auto &Class : Classes) {
2049*67e74705SXin Li if (Class->TheRecord == R)
2050*67e74705SXin Li return Class.get();
2051*67e74705SXin Li }
2052*67e74705SXin Li return nullptr;
2053*67e74705SXin Li }
2054*67e74705SXin Li
findSuperClass(Record * R) const2055*67e74705SXin Li AttrClass *findSuperClass(Record *R) const {
2056*67e74705SXin Li // TableGen flattens the superclass list, so we just need to walk it
2057*67e74705SXin Li // in reverse.
2058*67e74705SXin Li auto SuperClasses = R->getSuperClasses();
2059*67e74705SXin Li for (signed i = 0, e = SuperClasses.size(); i != e; ++i) {
2060*67e74705SXin Li auto SuperClass = findClassByRecord(SuperClasses[e - i - 1].first);
2061*67e74705SXin Li if (SuperClass) return SuperClass;
2062*67e74705SXin Li }
2063*67e74705SXin Li return nullptr;
2064*67e74705SXin Li }
2065*67e74705SXin Li };
2066*67e74705SXin Li
2067*67e74705SXin Li } // end anonymous namespace
2068*67e74705SXin Li
2069*67e74705SXin Li namespace clang {
2070*67e74705SXin Li
2071*67e74705SXin Li // Emits the enumeration list for attributes.
EmitClangAttrList(RecordKeeper & Records,raw_ostream & OS)2072*67e74705SXin Li void EmitClangAttrList(RecordKeeper &Records, raw_ostream &OS) {
2073*67e74705SXin Li emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
2074*67e74705SXin Li
2075*67e74705SXin Li AttrClassHierarchy Hierarchy(Records);
2076*67e74705SXin Li
2077*67e74705SXin Li // Add defaulting macro definitions.
2078*67e74705SXin Li Hierarchy.emitDefaultDefines(OS);
2079*67e74705SXin Li emitDefaultDefine(OS, "PRAGMA_SPELLING_ATTR", nullptr);
2080*67e74705SXin Li
2081*67e74705SXin Li std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
2082*67e74705SXin Li std::vector<Record *> PragmaAttrs;
2083*67e74705SXin Li for (auto *Attr : Attrs) {
2084*67e74705SXin Li if (!Attr->getValueAsBit("ASTNode"))
2085*67e74705SXin Li continue;
2086*67e74705SXin Li
2087*67e74705SXin Li // Add the attribute to the ad-hoc groups.
2088*67e74705SXin Li if (AttrHasPragmaSpelling(Attr))
2089*67e74705SXin Li PragmaAttrs.push_back(Attr);
2090*67e74705SXin Li
2091*67e74705SXin Li // Place it in the hierarchy.
2092*67e74705SXin Li Hierarchy.classifyAttr(Attr);
2093*67e74705SXin Li }
2094*67e74705SXin Li
2095*67e74705SXin Li // Emit the main attribute list.
2096*67e74705SXin Li Hierarchy.emitAttrLists(OS);
2097*67e74705SXin Li
2098*67e74705SXin Li // Emit the ad hoc groups.
2099*67e74705SXin Li emitAttrList(OS, "PRAGMA_SPELLING_ATTR", PragmaAttrs);
2100*67e74705SXin Li
2101*67e74705SXin Li // Emit the attribute ranges.
2102*67e74705SXin Li OS << "#ifdef ATTR_RANGE\n";
2103*67e74705SXin Li Hierarchy.emitAttrRanges(OS);
2104*67e74705SXin Li OS << "#undef ATTR_RANGE\n";
2105*67e74705SXin Li OS << "#endif\n";
2106*67e74705SXin Li
2107*67e74705SXin Li Hierarchy.emitUndefs(OS);
2108*67e74705SXin Li OS << "#undef PRAGMA_SPELLING_ATTR\n";
2109*67e74705SXin Li }
2110*67e74705SXin Li
2111*67e74705SXin Li // Emits the code to read an attribute from a precompiled header.
EmitClangAttrPCHRead(RecordKeeper & Records,raw_ostream & OS)2112*67e74705SXin Li void EmitClangAttrPCHRead(RecordKeeper &Records, raw_ostream &OS) {
2113*67e74705SXin Li emitSourceFileHeader("Attribute deserialization code", OS);
2114*67e74705SXin Li
2115*67e74705SXin Li Record *InhClass = Records.getClass("InheritableAttr");
2116*67e74705SXin Li std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
2117*67e74705SXin Li ArgRecords;
2118*67e74705SXin Li std::vector<std::unique_ptr<Argument>> Args;
2119*67e74705SXin Li
2120*67e74705SXin Li OS << " switch (Kind) {\n";
2121*67e74705SXin Li for (const auto *Attr : Attrs) {
2122*67e74705SXin Li const Record &R = *Attr;
2123*67e74705SXin Li if (!R.getValueAsBit("ASTNode"))
2124*67e74705SXin Li continue;
2125*67e74705SXin Li
2126*67e74705SXin Li OS << " case attr::" << R.getName() << ": {\n";
2127*67e74705SXin Li if (R.isSubClassOf(InhClass))
2128*67e74705SXin Li OS << " bool isInherited = Record[Idx++];\n";
2129*67e74705SXin Li OS << " bool isImplicit = Record[Idx++];\n";
2130*67e74705SXin Li OS << " unsigned Spelling = Record[Idx++];\n";
2131*67e74705SXin Li ArgRecords = R.getValueAsListOfDefs("Args");
2132*67e74705SXin Li Args.clear();
2133*67e74705SXin Li for (const auto *Arg : ArgRecords) {
2134*67e74705SXin Li Args.emplace_back(createArgument(*Arg, R.getName()));
2135*67e74705SXin Li Args.back()->writePCHReadDecls(OS);
2136*67e74705SXin Li }
2137*67e74705SXin Li OS << " New = new (Context) " << R.getName() << "Attr(Range, Context";
2138*67e74705SXin Li for (auto const &ri : Args) {
2139*67e74705SXin Li OS << ", ";
2140*67e74705SXin Li ri->writePCHReadArgs(OS);
2141*67e74705SXin Li }
2142*67e74705SXin Li OS << ", Spelling);\n";
2143*67e74705SXin Li if (R.isSubClassOf(InhClass))
2144*67e74705SXin Li OS << " cast<InheritableAttr>(New)->setInherited(isInherited);\n";
2145*67e74705SXin Li OS << " New->setImplicit(isImplicit);\n";
2146*67e74705SXin Li OS << " break;\n";
2147*67e74705SXin Li OS << " }\n";
2148*67e74705SXin Li }
2149*67e74705SXin Li OS << " }\n";
2150*67e74705SXin Li }
2151*67e74705SXin Li
2152*67e74705SXin Li // Emits the code to write an attribute to a precompiled header.
EmitClangAttrPCHWrite(RecordKeeper & Records,raw_ostream & OS)2153*67e74705SXin Li void EmitClangAttrPCHWrite(RecordKeeper &Records, raw_ostream &OS) {
2154*67e74705SXin Li emitSourceFileHeader("Attribute serialization code", OS);
2155*67e74705SXin Li
2156*67e74705SXin Li Record *InhClass = Records.getClass("InheritableAttr");
2157*67e74705SXin Li std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
2158*67e74705SXin Li
2159*67e74705SXin Li OS << " switch (A->getKind()) {\n";
2160*67e74705SXin Li for (const auto *Attr : Attrs) {
2161*67e74705SXin Li const Record &R = *Attr;
2162*67e74705SXin Li if (!R.getValueAsBit("ASTNode"))
2163*67e74705SXin Li continue;
2164*67e74705SXin Li OS << " case attr::" << R.getName() << ": {\n";
2165*67e74705SXin Li Args = R.getValueAsListOfDefs("Args");
2166*67e74705SXin Li if (R.isSubClassOf(InhClass) || !Args.empty())
2167*67e74705SXin Li OS << " const auto *SA = cast<" << R.getName()
2168*67e74705SXin Li << "Attr>(A);\n";
2169*67e74705SXin Li if (R.isSubClassOf(InhClass))
2170*67e74705SXin Li OS << " Record.push_back(SA->isInherited());\n";
2171*67e74705SXin Li OS << " Record.push_back(A->isImplicit());\n";
2172*67e74705SXin Li OS << " Record.push_back(A->getSpellingListIndex());\n";
2173*67e74705SXin Li
2174*67e74705SXin Li for (const auto *Arg : Args)
2175*67e74705SXin Li createArgument(*Arg, R.getName())->writePCHWrite(OS);
2176*67e74705SXin Li OS << " break;\n";
2177*67e74705SXin Li OS << " }\n";
2178*67e74705SXin Li }
2179*67e74705SXin Li OS << " }\n";
2180*67e74705SXin Li }
2181*67e74705SXin Li
2182*67e74705SXin Li // Generate a conditional expression to check if the current target satisfies
2183*67e74705SXin Li // the conditions for a TargetSpecificAttr record, and append the code for
2184*67e74705SXin Li // those checks to the Test string. If the FnName string pointer is non-null,
2185*67e74705SXin Li // append a unique suffix to distinguish this set of target checks from other
2186*67e74705SXin Li // TargetSpecificAttr records.
GenerateTargetSpecificAttrChecks(const Record * R,std::vector<std::string> & Arches,std::string & Test,std::string * FnName)2187*67e74705SXin Li static void GenerateTargetSpecificAttrChecks(const Record *R,
2188*67e74705SXin Li std::vector<std::string> &Arches,
2189*67e74705SXin Li std::string &Test,
2190*67e74705SXin Li std::string *FnName) {
2191*67e74705SXin Li // It is assumed that there will be an llvm::Triple object
2192*67e74705SXin Li // named "T" and a TargetInfo object named "Target" within
2193*67e74705SXin Li // scope that can be used to determine whether the attribute exists in
2194*67e74705SXin Li // a given target.
2195*67e74705SXin Li Test += "(";
2196*67e74705SXin Li
2197*67e74705SXin Li for (auto I = Arches.begin(), E = Arches.end(); I != E; ++I) {
2198*67e74705SXin Li std::string Part = *I;
2199*67e74705SXin Li Test += "T.getArch() == llvm::Triple::" + Part;
2200*67e74705SXin Li if (I + 1 != E)
2201*67e74705SXin Li Test += " || ";
2202*67e74705SXin Li if (FnName)
2203*67e74705SXin Li *FnName += Part;
2204*67e74705SXin Li }
2205*67e74705SXin Li Test += ")";
2206*67e74705SXin Li
2207*67e74705SXin Li // If the attribute is specific to particular OSes, check those.
2208*67e74705SXin Li if (!R->isValueUnset("OSes")) {
2209*67e74705SXin Li // We know that there was at least one arch test, so we need to and in the
2210*67e74705SXin Li // OS tests.
2211*67e74705SXin Li Test += " && (";
2212*67e74705SXin Li std::vector<std::string> OSes = R->getValueAsListOfStrings("OSes");
2213*67e74705SXin Li for (auto I = OSes.begin(), E = OSes.end(); I != E; ++I) {
2214*67e74705SXin Li std::string Part = *I;
2215*67e74705SXin Li
2216*67e74705SXin Li Test += "T.getOS() == llvm::Triple::" + Part;
2217*67e74705SXin Li if (I + 1 != E)
2218*67e74705SXin Li Test += " || ";
2219*67e74705SXin Li if (FnName)
2220*67e74705SXin Li *FnName += Part;
2221*67e74705SXin Li }
2222*67e74705SXin Li Test += ")";
2223*67e74705SXin Li }
2224*67e74705SXin Li
2225*67e74705SXin Li // If one or more CXX ABIs are specified, check those as well.
2226*67e74705SXin Li if (!R->isValueUnset("CXXABIs")) {
2227*67e74705SXin Li Test += " && (";
2228*67e74705SXin Li std::vector<std::string> CXXABIs = R->getValueAsListOfStrings("CXXABIs");
2229*67e74705SXin Li for (auto I = CXXABIs.begin(), E = CXXABIs.end(); I != E; ++I) {
2230*67e74705SXin Li std::string Part = *I;
2231*67e74705SXin Li Test += "Target.getCXXABI().getKind() == TargetCXXABI::" + Part;
2232*67e74705SXin Li if (I + 1 != E)
2233*67e74705SXin Li Test += " || ";
2234*67e74705SXin Li if (FnName)
2235*67e74705SXin Li *FnName += Part;
2236*67e74705SXin Li }
2237*67e74705SXin Li Test += ")";
2238*67e74705SXin Li }
2239*67e74705SXin Li }
2240*67e74705SXin Li
GenerateHasAttrSpellingStringSwitch(const std::vector<Record * > & Attrs,raw_ostream & OS,const std::string & Variety="",const std::string & Scope="")2241*67e74705SXin Li static void GenerateHasAttrSpellingStringSwitch(
2242*67e74705SXin Li const std::vector<Record *> &Attrs, raw_ostream &OS,
2243*67e74705SXin Li const std::string &Variety = "", const std::string &Scope = "") {
2244*67e74705SXin Li for (const auto *Attr : Attrs) {
2245*67e74705SXin Li // C++11-style attributes have specific version information associated with
2246*67e74705SXin Li // them. If the attribute has no scope, the version information must not
2247*67e74705SXin Li // have the default value (1), as that's incorrect. Instead, the unscoped
2248*67e74705SXin Li // attribute version information should be taken from the SD-6 standing
2249*67e74705SXin Li // document, which can be found at:
2250*67e74705SXin Li // https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations
2251*67e74705SXin Li int Version = 1;
2252*67e74705SXin Li
2253*67e74705SXin Li if (Variety == "CXX11") {
2254*67e74705SXin Li std::vector<Record *> Spellings = Attr->getValueAsListOfDefs("Spellings");
2255*67e74705SXin Li for (const auto &Spelling : Spellings) {
2256*67e74705SXin Li if (Spelling->getValueAsString("Variety") == "CXX11") {
2257*67e74705SXin Li Version = static_cast<int>(Spelling->getValueAsInt("Version"));
2258*67e74705SXin Li if (Scope.empty() && Version == 1)
2259*67e74705SXin Li PrintError(Spelling->getLoc(), "C++ standard attributes must "
2260*67e74705SXin Li "have valid version information.");
2261*67e74705SXin Li break;
2262*67e74705SXin Li }
2263*67e74705SXin Li }
2264*67e74705SXin Li }
2265*67e74705SXin Li
2266*67e74705SXin Li std::string Test;
2267*67e74705SXin Li if (Attr->isSubClassOf("TargetSpecificAttr")) {
2268*67e74705SXin Li const Record *R = Attr->getValueAsDef("Target");
2269*67e74705SXin Li std::vector<std::string> Arches = R->getValueAsListOfStrings("Arches");
2270*67e74705SXin Li GenerateTargetSpecificAttrChecks(R, Arches, Test, nullptr);
2271*67e74705SXin Li
2272*67e74705SXin Li // If this is the C++11 variety, also add in the LangOpts test.
2273*67e74705SXin Li if (Variety == "CXX11")
2274*67e74705SXin Li Test += " && LangOpts.CPlusPlus11";
2275*67e74705SXin Li } else if (Variety == "CXX11")
2276*67e74705SXin Li // C++11 mode should be checked against LangOpts, which is presumed to be
2277*67e74705SXin Li // present in the caller.
2278*67e74705SXin Li Test = "LangOpts.CPlusPlus11";
2279*67e74705SXin Li
2280*67e74705SXin Li std::string TestStr =
2281*67e74705SXin Li !Test.empty() ? Test + " ? " + llvm::itostr(Version) + " : 0" : "1";
2282*67e74705SXin Li std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
2283*67e74705SXin Li for (const auto &S : Spellings)
2284*67e74705SXin Li if (Variety.empty() || (Variety == S.variety() &&
2285*67e74705SXin Li (Scope.empty() || Scope == S.nameSpace())))
2286*67e74705SXin Li OS << " .Case(\"" << S.name() << "\", " << TestStr << ")\n";
2287*67e74705SXin Li }
2288*67e74705SXin Li OS << " .Default(0);\n";
2289*67e74705SXin Li }
2290*67e74705SXin Li
2291*67e74705SXin Li // Emits the list of spellings for attributes.
EmitClangAttrHasAttrImpl(RecordKeeper & Records,raw_ostream & OS)2292*67e74705SXin Li void EmitClangAttrHasAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
2293*67e74705SXin Li emitSourceFileHeader("Code to implement the __has_attribute logic", OS);
2294*67e74705SXin Li
2295*67e74705SXin Li // Separate all of the attributes out into four group: generic, C++11, GNU,
2296*67e74705SXin Li // and declspecs. Then generate a big switch statement for each of them.
2297*67e74705SXin Li std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
2298*67e74705SXin Li std::vector<Record *> Declspec, GNU, Pragma;
2299*67e74705SXin Li std::map<std::string, std::vector<Record *>> CXX;
2300*67e74705SXin Li
2301*67e74705SXin Li // Walk over the list of all attributes, and split them out based on the
2302*67e74705SXin Li // spelling variety.
2303*67e74705SXin Li for (auto *R : Attrs) {
2304*67e74705SXin Li std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R);
2305*67e74705SXin Li for (const auto &SI : Spellings) {
2306*67e74705SXin Li const std::string &Variety = SI.variety();
2307*67e74705SXin Li if (Variety == "GNU")
2308*67e74705SXin Li GNU.push_back(R);
2309*67e74705SXin Li else if (Variety == "Declspec")
2310*67e74705SXin Li Declspec.push_back(R);
2311*67e74705SXin Li else if (Variety == "CXX11")
2312*67e74705SXin Li CXX[SI.nameSpace()].push_back(R);
2313*67e74705SXin Li else if (Variety == "Pragma")
2314*67e74705SXin Li Pragma.push_back(R);
2315*67e74705SXin Li }
2316*67e74705SXin Li }
2317*67e74705SXin Li
2318*67e74705SXin Li OS << "const llvm::Triple &T = Target.getTriple();\n";
2319*67e74705SXin Li OS << "switch (Syntax) {\n";
2320*67e74705SXin Li OS << "case AttrSyntax::GNU:\n";
2321*67e74705SXin Li OS << " return llvm::StringSwitch<int>(Name)\n";
2322*67e74705SXin Li GenerateHasAttrSpellingStringSwitch(GNU, OS, "GNU");
2323*67e74705SXin Li OS << "case AttrSyntax::Declspec:\n";
2324*67e74705SXin Li OS << " return llvm::StringSwitch<int>(Name)\n";
2325*67e74705SXin Li GenerateHasAttrSpellingStringSwitch(Declspec, OS, "Declspec");
2326*67e74705SXin Li OS << "case AttrSyntax::Pragma:\n";
2327*67e74705SXin Li OS << " return llvm::StringSwitch<int>(Name)\n";
2328*67e74705SXin Li GenerateHasAttrSpellingStringSwitch(Pragma, OS, "Pragma");
2329*67e74705SXin Li OS << "case AttrSyntax::CXX: {\n";
2330*67e74705SXin Li // C++11-style attributes are further split out based on the Scope.
2331*67e74705SXin Li for (auto I = CXX.cbegin(), E = CXX.cend(); I != E; ++I) {
2332*67e74705SXin Li if (I != CXX.begin())
2333*67e74705SXin Li OS << " else ";
2334*67e74705SXin Li if (I->first.empty())
2335*67e74705SXin Li OS << "if (!Scope || Scope->getName() == \"\") {\n";
2336*67e74705SXin Li else
2337*67e74705SXin Li OS << "if (Scope->getName() == \"" << I->first << "\") {\n";
2338*67e74705SXin Li OS << " return llvm::StringSwitch<int>(Name)\n";
2339*67e74705SXin Li GenerateHasAttrSpellingStringSwitch(I->second, OS, "CXX11", I->first);
2340*67e74705SXin Li OS << "}";
2341*67e74705SXin Li }
2342*67e74705SXin Li OS << "\n}\n";
2343*67e74705SXin Li OS << "}\n";
2344*67e74705SXin Li }
2345*67e74705SXin Li
EmitClangAttrSpellingListIndex(RecordKeeper & Records,raw_ostream & OS)2346*67e74705SXin Li void EmitClangAttrSpellingListIndex(RecordKeeper &Records, raw_ostream &OS) {
2347*67e74705SXin Li emitSourceFileHeader("Code to translate different attribute spellings "
2348*67e74705SXin Li "into internal identifiers", OS);
2349*67e74705SXin Li
2350*67e74705SXin Li OS << " switch (AttrKind) {\n";
2351*67e74705SXin Li
2352*67e74705SXin Li ParsedAttrMap Attrs = getParsedAttrList(Records);
2353*67e74705SXin Li for (const auto &I : Attrs) {
2354*67e74705SXin Li const Record &R = *I.second;
2355*67e74705SXin Li std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
2356*67e74705SXin Li OS << " case AT_" << I.first << ": {\n";
2357*67e74705SXin Li for (unsigned I = 0; I < Spellings.size(); ++ I) {
2358*67e74705SXin Li OS << " if (Name == \"" << Spellings[I].name() << "\" && "
2359*67e74705SXin Li << "SyntaxUsed == "
2360*67e74705SXin Li << StringSwitch<unsigned>(Spellings[I].variety())
2361*67e74705SXin Li .Case("GNU", 0)
2362*67e74705SXin Li .Case("CXX11", 1)
2363*67e74705SXin Li .Case("Declspec", 2)
2364*67e74705SXin Li .Case("Keyword", 3)
2365*67e74705SXin Li .Case("Pragma", 4)
2366*67e74705SXin Li .Default(0)
2367*67e74705SXin Li << " && Scope == \"" << Spellings[I].nameSpace() << "\")\n"
2368*67e74705SXin Li << " return " << I << ";\n";
2369*67e74705SXin Li }
2370*67e74705SXin Li
2371*67e74705SXin Li OS << " break;\n";
2372*67e74705SXin Li OS << " }\n";
2373*67e74705SXin Li }
2374*67e74705SXin Li
2375*67e74705SXin Li OS << " }\n";
2376*67e74705SXin Li OS << " return 0;\n";
2377*67e74705SXin Li }
2378*67e74705SXin Li
2379*67e74705SXin Li // Emits code used by RecursiveASTVisitor to visit attributes
EmitClangAttrASTVisitor(RecordKeeper & Records,raw_ostream & OS)2380*67e74705SXin Li void EmitClangAttrASTVisitor(RecordKeeper &Records, raw_ostream &OS) {
2381*67e74705SXin Li emitSourceFileHeader("Used by RecursiveASTVisitor to visit attributes.", OS);
2382*67e74705SXin Li
2383*67e74705SXin Li std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
2384*67e74705SXin Li
2385*67e74705SXin Li // Write method declarations for Traverse* methods.
2386*67e74705SXin Li // We emit this here because we only generate methods for attributes that
2387*67e74705SXin Li // are declared as ASTNodes.
2388*67e74705SXin Li OS << "#ifdef ATTR_VISITOR_DECLS_ONLY\n\n";
2389*67e74705SXin Li for (const auto *Attr : Attrs) {
2390*67e74705SXin Li const Record &R = *Attr;
2391*67e74705SXin Li if (!R.getValueAsBit("ASTNode"))
2392*67e74705SXin Li continue;
2393*67e74705SXin Li OS << " bool Traverse"
2394*67e74705SXin Li << R.getName() << "Attr(" << R.getName() << "Attr *A);\n";
2395*67e74705SXin Li OS << " bool Visit"
2396*67e74705SXin Li << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n"
2397*67e74705SXin Li << " return true; \n"
2398*67e74705SXin Li << " }\n";
2399*67e74705SXin Li }
2400*67e74705SXin Li OS << "\n#else // ATTR_VISITOR_DECLS_ONLY\n\n";
2401*67e74705SXin Li
2402*67e74705SXin Li // Write individual Traverse* methods for each attribute class.
2403*67e74705SXin Li for (const auto *Attr : Attrs) {
2404*67e74705SXin Li const Record &R = *Attr;
2405*67e74705SXin Li if (!R.getValueAsBit("ASTNode"))
2406*67e74705SXin Li continue;
2407*67e74705SXin Li
2408*67e74705SXin Li OS << "template <typename Derived>\n"
2409*67e74705SXin Li << "bool VISITORCLASS<Derived>::Traverse"
2410*67e74705SXin Li << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n"
2411*67e74705SXin Li << " if (!getDerived().VisitAttr(A))\n"
2412*67e74705SXin Li << " return false;\n"
2413*67e74705SXin Li << " if (!getDerived().Visit" << R.getName() << "Attr(A))\n"
2414*67e74705SXin Li << " return false;\n";
2415*67e74705SXin Li
2416*67e74705SXin Li std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
2417*67e74705SXin Li for (const auto *Arg : ArgRecords)
2418*67e74705SXin Li createArgument(*Arg, R.getName())->writeASTVisitorTraversal(OS);
2419*67e74705SXin Li
2420*67e74705SXin Li OS << " return true;\n";
2421*67e74705SXin Li OS << "}\n\n";
2422*67e74705SXin Li }
2423*67e74705SXin Li
2424*67e74705SXin Li // Write generic Traverse routine
2425*67e74705SXin Li OS << "template <typename Derived>\n"
2426*67e74705SXin Li << "bool VISITORCLASS<Derived>::TraverseAttr(Attr *A) {\n"
2427*67e74705SXin Li << " if (!A)\n"
2428*67e74705SXin Li << " return true;\n"
2429*67e74705SXin Li << "\n"
2430*67e74705SXin Li << " switch (A->getKind()) {\n";
2431*67e74705SXin Li
2432*67e74705SXin Li for (const auto *Attr : Attrs) {
2433*67e74705SXin Li const Record &R = *Attr;
2434*67e74705SXin Li if (!R.getValueAsBit("ASTNode"))
2435*67e74705SXin Li continue;
2436*67e74705SXin Li
2437*67e74705SXin Li OS << " case attr::" << R.getName() << ":\n"
2438*67e74705SXin Li << " return getDerived().Traverse" << R.getName() << "Attr("
2439*67e74705SXin Li << "cast<" << R.getName() << "Attr>(A));\n";
2440*67e74705SXin Li }
2441*67e74705SXin Li OS << " }\n"; // end switch
2442*67e74705SXin Li OS << " llvm_unreachable(\"bad attribute kind\");\n";
2443*67e74705SXin Li OS << "}\n"; // end function
2444*67e74705SXin Li OS << "#endif // ATTR_VISITOR_DECLS_ONLY\n";
2445*67e74705SXin Li }
2446*67e74705SXin Li
2447*67e74705SXin Li // Emits code to instantiate dependent attributes on templates.
EmitClangAttrTemplateInstantiate(RecordKeeper & Records,raw_ostream & OS)2448*67e74705SXin Li void EmitClangAttrTemplateInstantiate(RecordKeeper &Records, raw_ostream &OS) {
2449*67e74705SXin Li emitSourceFileHeader("Template instantiation code for attributes", OS);
2450*67e74705SXin Li
2451*67e74705SXin Li std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
2452*67e74705SXin Li
2453*67e74705SXin Li OS << "namespace clang {\n"
2454*67e74705SXin Li << "namespace sema {\n\n"
2455*67e74705SXin Li << "Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, "
2456*67e74705SXin Li << "Sema &S,\n"
2457*67e74705SXin Li << " const MultiLevelTemplateArgumentList &TemplateArgs) {\n"
2458*67e74705SXin Li << " switch (At->getKind()) {\n";
2459*67e74705SXin Li
2460*67e74705SXin Li for (const auto *Attr : Attrs) {
2461*67e74705SXin Li const Record &R = *Attr;
2462*67e74705SXin Li if (!R.getValueAsBit("ASTNode"))
2463*67e74705SXin Li continue;
2464*67e74705SXin Li
2465*67e74705SXin Li OS << " case attr::" << R.getName() << ": {\n";
2466*67e74705SXin Li bool ShouldClone = R.getValueAsBit("Clone");
2467*67e74705SXin Li
2468*67e74705SXin Li if (!ShouldClone) {
2469*67e74705SXin Li OS << " return nullptr;\n";
2470*67e74705SXin Li OS << " }\n";
2471*67e74705SXin Li continue;
2472*67e74705SXin Li }
2473*67e74705SXin Li
2474*67e74705SXin Li OS << " const auto *A = cast<"
2475*67e74705SXin Li << R.getName() << "Attr>(At);\n";
2476*67e74705SXin Li bool TDependent = R.getValueAsBit("TemplateDependent");
2477*67e74705SXin Li
2478*67e74705SXin Li if (!TDependent) {
2479*67e74705SXin Li OS << " return A->clone(C);\n";
2480*67e74705SXin Li OS << " }\n";
2481*67e74705SXin Li continue;
2482*67e74705SXin Li }
2483*67e74705SXin Li
2484*67e74705SXin Li std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
2485*67e74705SXin Li std::vector<std::unique_ptr<Argument>> Args;
2486*67e74705SXin Li Args.reserve(ArgRecords.size());
2487*67e74705SXin Li
2488*67e74705SXin Li for (const auto *ArgRecord : ArgRecords)
2489*67e74705SXin Li Args.emplace_back(createArgument(*ArgRecord, R.getName()));
2490*67e74705SXin Li
2491*67e74705SXin Li for (auto const &ai : Args)
2492*67e74705SXin Li ai->writeTemplateInstantiation(OS);
2493*67e74705SXin Li
2494*67e74705SXin Li OS << " return new (C) " << R.getName() << "Attr(A->getLocation(), C";
2495*67e74705SXin Li for (auto const &ai : Args) {
2496*67e74705SXin Li OS << ", ";
2497*67e74705SXin Li ai->writeTemplateInstantiationArgs(OS);
2498*67e74705SXin Li }
2499*67e74705SXin Li OS << ", A->getSpellingListIndex());\n }\n";
2500*67e74705SXin Li }
2501*67e74705SXin Li OS << " } // end switch\n"
2502*67e74705SXin Li << " llvm_unreachable(\"Unknown attribute!\");\n"
2503*67e74705SXin Li << " return nullptr;\n"
2504*67e74705SXin Li << "}\n\n"
2505*67e74705SXin Li << "} // end namespace sema\n"
2506*67e74705SXin Li << "} // end namespace clang\n";
2507*67e74705SXin Li }
2508*67e74705SXin Li
2509*67e74705SXin Li // Emits the list of parsed attributes.
EmitClangAttrParsedAttrList(RecordKeeper & Records,raw_ostream & OS)2510*67e74705SXin Li void EmitClangAttrParsedAttrList(RecordKeeper &Records, raw_ostream &OS) {
2511*67e74705SXin Li emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
2512*67e74705SXin Li
2513*67e74705SXin Li OS << "#ifndef PARSED_ATTR\n";
2514*67e74705SXin Li OS << "#define PARSED_ATTR(NAME) NAME\n";
2515*67e74705SXin Li OS << "#endif\n\n";
2516*67e74705SXin Li
2517*67e74705SXin Li ParsedAttrMap Names = getParsedAttrList(Records);
2518*67e74705SXin Li for (const auto &I : Names) {
2519*67e74705SXin Li OS << "PARSED_ATTR(" << I.first << ")\n";
2520*67e74705SXin Li }
2521*67e74705SXin Li }
2522*67e74705SXin Li
isArgVariadic(const Record & R,StringRef AttrName)2523*67e74705SXin Li static bool isArgVariadic(const Record &R, StringRef AttrName) {
2524*67e74705SXin Li return createArgument(R, AttrName)->isVariadic();
2525*67e74705SXin Li }
2526*67e74705SXin Li
emitArgInfo(const Record & R,std::stringstream & OS)2527*67e74705SXin Li static void emitArgInfo(const Record &R, std::stringstream &OS) {
2528*67e74705SXin Li // This function will count the number of arguments specified for the
2529*67e74705SXin Li // attribute and emit the number of required arguments followed by the
2530*67e74705SXin Li // number of optional arguments.
2531*67e74705SXin Li std::vector<Record *> Args = R.getValueAsListOfDefs("Args");
2532*67e74705SXin Li unsigned ArgCount = 0, OptCount = 0;
2533*67e74705SXin Li bool HasVariadic = false;
2534*67e74705SXin Li for (const auto *Arg : Args) {
2535*67e74705SXin Li Arg->getValueAsBit("Optional") ? ++OptCount : ++ArgCount;
2536*67e74705SXin Li if (!HasVariadic && isArgVariadic(*Arg, R.getName()))
2537*67e74705SXin Li HasVariadic = true;
2538*67e74705SXin Li }
2539*67e74705SXin Li
2540*67e74705SXin Li // If there is a variadic argument, we will set the optional argument count
2541*67e74705SXin Li // to its largest value. Since it's currently a 4-bit number, we set it to 15.
2542*67e74705SXin Li OS << ArgCount << ", " << (HasVariadic ? 15 : OptCount);
2543*67e74705SXin Li }
2544*67e74705SXin Li
GenerateDefaultAppertainsTo(raw_ostream & OS)2545*67e74705SXin Li static void GenerateDefaultAppertainsTo(raw_ostream &OS) {
2546*67e74705SXin Li OS << "static bool defaultAppertainsTo(Sema &, const AttributeList &,";
2547*67e74705SXin Li OS << "const Decl *) {\n";
2548*67e74705SXin Li OS << " return true;\n";
2549*67e74705SXin Li OS << "}\n\n";
2550*67e74705SXin Li }
2551*67e74705SXin Li
CalculateDiagnostic(const Record & S)2552*67e74705SXin Li static std::string CalculateDiagnostic(const Record &S) {
2553*67e74705SXin Li // If the SubjectList object has a custom diagnostic associated with it,
2554*67e74705SXin Li // return that directly.
2555*67e74705SXin Li std::string CustomDiag = S.getValueAsString("CustomDiag");
2556*67e74705SXin Li if (!CustomDiag.empty())
2557*67e74705SXin Li return CustomDiag;
2558*67e74705SXin Li
2559*67e74705SXin Li // Given the list of subjects, determine what diagnostic best fits.
2560*67e74705SXin Li enum {
2561*67e74705SXin Li Func = 1U << 0,
2562*67e74705SXin Li Var = 1U << 1,
2563*67e74705SXin Li ObjCMethod = 1U << 2,
2564*67e74705SXin Li Param = 1U << 3,
2565*67e74705SXin Li Class = 1U << 4,
2566*67e74705SXin Li GenericRecord = 1U << 5,
2567*67e74705SXin Li Type = 1U << 6,
2568*67e74705SXin Li ObjCIVar = 1U << 7,
2569*67e74705SXin Li ObjCProp = 1U << 8,
2570*67e74705SXin Li ObjCInterface = 1U << 9,
2571*67e74705SXin Li Block = 1U << 10,
2572*67e74705SXin Li Namespace = 1U << 11,
2573*67e74705SXin Li Field = 1U << 12,
2574*67e74705SXin Li CXXMethod = 1U << 13,
2575*67e74705SXin Li ObjCProtocol = 1U << 14,
2576*67e74705SXin Li Enum = 1U << 15
2577*67e74705SXin Li };
2578*67e74705SXin Li uint32_t SubMask = 0;
2579*67e74705SXin Li
2580*67e74705SXin Li std::vector<Record *> Subjects = S.getValueAsListOfDefs("Subjects");
2581*67e74705SXin Li for (const auto *Subject : Subjects) {
2582*67e74705SXin Li const Record &R = *Subject;
2583*67e74705SXin Li std::string Name;
2584*67e74705SXin Li
2585*67e74705SXin Li if (R.isSubClassOf("SubsetSubject")) {
2586*67e74705SXin Li PrintError(R.getLoc(), "SubsetSubjects should use a custom diagnostic");
2587*67e74705SXin Li // As a fallback, look through the SubsetSubject to see what its base
2588*67e74705SXin Li // type is, and use that. This needs to be updated if SubsetSubjects
2589*67e74705SXin Li // are allowed within other SubsetSubjects.
2590*67e74705SXin Li Name = R.getValueAsDef("Base")->getName();
2591*67e74705SXin Li } else
2592*67e74705SXin Li Name = R.getName();
2593*67e74705SXin Li
2594*67e74705SXin Li uint32_t V = StringSwitch<uint32_t>(Name)
2595*67e74705SXin Li .Case("Function", Func)
2596*67e74705SXin Li .Case("Var", Var)
2597*67e74705SXin Li .Case("ObjCMethod", ObjCMethod)
2598*67e74705SXin Li .Case("ParmVar", Param)
2599*67e74705SXin Li .Case("TypedefName", Type)
2600*67e74705SXin Li .Case("ObjCIvar", ObjCIVar)
2601*67e74705SXin Li .Case("ObjCProperty", ObjCProp)
2602*67e74705SXin Li .Case("Record", GenericRecord)
2603*67e74705SXin Li .Case("ObjCInterface", ObjCInterface)
2604*67e74705SXin Li .Case("ObjCProtocol", ObjCProtocol)
2605*67e74705SXin Li .Case("Block", Block)
2606*67e74705SXin Li .Case("CXXRecord", Class)
2607*67e74705SXin Li .Case("Namespace", Namespace)
2608*67e74705SXin Li .Case("Field", Field)
2609*67e74705SXin Li .Case("CXXMethod", CXXMethod)
2610*67e74705SXin Li .Case("Enum", Enum)
2611*67e74705SXin Li .Default(0);
2612*67e74705SXin Li if (!V) {
2613*67e74705SXin Li // Something wasn't in our mapping, so be helpful and let the developer
2614*67e74705SXin Li // know about it.
2615*67e74705SXin Li PrintFatalError(R.getLoc(), "Unknown subject type: " + R.getName());
2616*67e74705SXin Li return "";
2617*67e74705SXin Li }
2618*67e74705SXin Li
2619*67e74705SXin Li SubMask |= V;
2620*67e74705SXin Li }
2621*67e74705SXin Li
2622*67e74705SXin Li switch (SubMask) {
2623*67e74705SXin Li // For the simple cases where there's only a single entry in the mask, we
2624*67e74705SXin Li // don't have to resort to bit fiddling.
2625*67e74705SXin Li case Func: return "ExpectedFunction";
2626*67e74705SXin Li case Var: return "ExpectedVariable";
2627*67e74705SXin Li case Param: return "ExpectedParameter";
2628*67e74705SXin Li case Class: return "ExpectedClass";
2629*67e74705SXin Li case Enum: return "ExpectedEnum";
2630*67e74705SXin Li case CXXMethod:
2631*67e74705SXin Li // FIXME: Currently, this maps to ExpectedMethod based on existing code,
2632*67e74705SXin Li // but should map to something a bit more accurate at some point.
2633*67e74705SXin Li case ObjCMethod: return "ExpectedMethod";
2634*67e74705SXin Li case Type: return "ExpectedType";
2635*67e74705SXin Li case ObjCInterface: return "ExpectedObjectiveCInterface";
2636*67e74705SXin Li case ObjCProtocol: return "ExpectedObjectiveCProtocol";
2637*67e74705SXin Li
2638*67e74705SXin Li // "GenericRecord" means struct, union or class; check the language options
2639*67e74705SXin Li // and if not compiling for C++, strip off the class part. Note that this
2640*67e74705SXin Li // relies on the fact that the context for this declares "Sema &S".
2641*67e74705SXin Li case GenericRecord:
2642*67e74705SXin Li return "(S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass : "
2643*67e74705SXin Li "ExpectedStructOrUnion)";
2644*67e74705SXin Li case Func | ObjCMethod | Block: return "ExpectedFunctionMethodOrBlock";
2645*67e74705SXin Li case Func | ObjCMethod | Class: return "ExpectedFunctionMethodOrClass";
2646*67e74705SXin Li case Func | Param:
2647*67e74705SXin Li case Func | ObjCMethod | Param: return "ExpectedFunctionMethodOrParameter";
2648*67e74705SXin Li case Func | ObjCMethod: return "ExpectedFunctionOrMethod";
2649*67e74705SXin Li case Func | Var: return "ExpectedVariableOrFunction";
2650*67e74705SXin Li
2651*67e74705SXin Li // If not compiling for C++, the class portion does not apply.
2652*67e74705SXin Li case Func | Var | Class:
2653*67e74705SXin Li return "(S.getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass : "
2654*67e74705SXin Li "ExpectedVariableOrFunction)";
2655*67e74705SXin Li
2656*67e74705SXin Li case ObjCMethod | ObjCProp: return "ExpectedMethodOrProperty";
2657*67e74705SXin Li case ObjCProtocol | ObjCInterface:
2658*67e74705SXin Li return "ExpectedObjectiveCInterfaceOrProtocol";
2659*67e74705SXin Li case Field | Var: return "ExpectedFieldOrGlobalVar";
2660*67e74705SXin Li }
2661*67e74705SXin Li
2662*67e74705SXin Li PrintFatalError(S.getLoc(),
2663*67e74705SXin Li "Could not deduce diagnostic argument for Attr subjects");
2664*67e74705SXin Li
2665*67e74705SXin Li return "";
2666*67e74705SXin Li }
2667*67e74705SXin Li
GetSubjectWithSuffix(const Record * R)2668*67e74705SXin Li static std::string GetSubjectWithSuffix(const Record *R) {
2669*67e74705SXin Li std::string B = R->getName();
2670*67e74705SXin Li if (B == "DeclBase")
2671*67e74705SXin Li return "Decl";
2672*67e74705SXin Li return B + "Decl";
2673*67e74705SXin Li }
2674*67e74705SXin Li
GenerateCustomAppertainsTo(const Record & Subject,raw_ostream & OS)2675*67e74705SXin Li static std::string GenerateCustomAppertainsTo(const Record &Subject,
2676*67e74705SXin Li raw_ostream &OS) {
2677*67e74705SXin Li std::string FnName = "is" + Subject.getName();
2678*67e74705SXin Li
2679*67e74705SXin Li // If this code has already been generated, simply return the previous
2680*67e74705SXin Li // instance of it.
2681*67e74705SXin Li static std::set<std::string> CustomSubjectSet;
2682*67e74705SXin Li auto I = CustomSubjectSet.find(FnName);
2683*67e74705SXin Li if (I != CustomSubjectSet.end())
2684*67e74705SXin Li return *I;
2685*67e74705SXin Li
2686*67e74705SXin Li Record *Base = Subject.getValueAsDef("Base");
2687*67e74705SXin Li
2688*67e74705SXin Li // Not currently support custom subjects within custom subjects.
2689*67e74705SXin Li if (Base->isSubClassOf("SubsetSubject")) {
2690*67e74705SXin Li PrintFatalError(Subject.getLoc(),
2691*67e74705SXin Li "SubsetSubjects within SubsetSubjects is not supported");
2692*67e74705SXin Li return "";
2693*67e74705SXin Li }
2694*67e74705SXin Li
2695*67e74705SXin Li OS << "static bool " << FnName << "(const Decl *D) {\n";
2696*67e74705SXin Li OS << " if (const auto *S = dyn_cast<";
2697*67e74705SXin Li OS << GetSubjectWithSuffix(Base);
2698*67e74705SXin Li OS << ">(D))\n";
2699*67e74705SXin Li OS << " return " << Subject.getValueAsString("CheckCode") << ";\n";
2700*67e74705SXin Li OS << " return false;\n";
2701*67e74705SXin Li OS << "}\n\n";
2702*67e74705SXin Li
2703*67e74705SXin Li CustomSubjectSet.insert(FnName);
2704*67e74705SXin Li return FnName;
2705*67e74705SXin Li }
2706*67e74705SXin Li
GenerateAppertainsTo(const Record & Attr,raw_ostream & OS)2707*67e74705SXin Li static std::string GenerateAppertainsTo(const Record &Attr, raw_ostream &OS) {
2708*67e74705SXin Li // If the attribute does not contain a Subjects definition, then use the
2709*67e74705SXin Li // default appertainsTo logic.
2710*67e74705SXin Li if (Attr.isValueUnset("Subjects"))
2711*67e74705SXin Li return "defaultAppertainsTo";
2712*67e74705SXin Li
2713*67e74705SXin Li const Record *SubjectObj = Attr.getValueAsDef("Subjects");
2714*67e74705SXin Li std::vector<Record*> Subjects = SubjectObj->getValueAsListOfDefs("Subjects");
2715*67e74705SXin Li
2716*67e74705SXin Li // If the list of subjects is empty, it is assumed that the attribute
2717*67e74705SXin Li // appertains to everything.
2718*67e74705SXin Li if (Subjects.empty())
2719*67e74705SXin Li return "defaultAppertainsTo";
2720*67e74705SXin Li
2721*67e74705SXin Li bool Warn = SubjectObj->getValueAsDef("Diag")->getValueAsBit("Warn");
2722*67e74705SXin Li
2723*67e74705SXin Li // Otherwise, generate an appertainsTo check specific to this attribute which
2724*67e74705SXin Li // checks all of the given subjects against the Decl passed in. Return the
2725*67e74705SXin Li // name of that check to the caller.
2726*67e74705SXin Li std::string FnName = "check" + Attr.getName() + "AppertainsTo";
2727*67e74705SXin Li std::stringstream SS;
2728*67e74705SXin Li SS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr, ";
2729*67e74705SXin Li SS << "const Decl *D) {\n";
2730*67e74705SXin Li SS << " if (";
2731*67e74705SXin Li for (auto I = Subjects.begin(), E = Subjects.end(); I != E; ++I) {
2732*67e74705SXin Li // If the subject has custom code associated with it, generate a function
2733*67e74705SXin Li // for it. The function cannot be inlined into this check (yet) because it
2734*67e74705SXin Li // requires the subject to be of a specific type, and were that information
2735*67e74705SXin Li // inlined here, it would not support an attribute with multiple custom
2736*67e74705SXin Li // subjects.
2737*67e74705SXin Li if ((*I)->isSubClassOf("SubsetSubject")) {
2738*67e74705SXin Li SS << "!" << GenerateCustomAppertainsTo(**I, OS) << "(D)";
2739*67e74705SXin Li } else {
2740*67e74705SXin Li SS << "!isa<" << GetSubjectWithSuffix(*I) << ">(D)";
2741*67e74705SXin Li }
2742*67e74705SXin Li
2743*67e74705SXin Li if (I + 1 != E)
2744*67e74705SXin Li SS << " && ";
2745*67e74705SXin Li }
2746*67e74705SXin Li SS << ") {\n";
2747*67e74705SXin Li SS << " S.Diag(Attr.getLoc(), diag::";
2748*67e74705SXin Li SS << (Warn ? "warn_attribute_wrong_decl_type" :
2749*67e74705SXin Li "err_attribute_wrong_decl_type");
2750*67e74705SXin Li SS << ")\n";
2751*67e74705SXin Li SS << " << Attr.getName() << ";
2752*67e74705SXin Li SS << CalculateDiagnostic(*SubjectObj) << ";\n";
2753*67e74705SXin Li SS << " return false;\n";
2754*67e74705SXin Li SS << " }\n";
2755*67e74705SXin Li SS << " return true;\n";
2756*67e74705SXin Li SS << "}\n\n";
2757*67e74705SXin Li
2758*67e74705SXin Li OS << SS.str();
2759*67e74705SXin Li return FnName;
2760*67e74705SXin Li }
2761*67e74705SXin Li
GenerateDefaultLangOptRequirements(raw_ostream & OS)2762*67e74705SXin Li static void GenerateDefaultLangOptRequirements(raw_ostream &OS) {
2763*67e74705SXin Li OS << "static bool defaultDiagnoseLangOpts(Sema &, ";
2764*67e74705SXin Li OS << "const AttributeList &) {\n";
2765*67e74705SXin Li OS << " return true;\n";
2766*67e74705SXin Li OS << "}\n\n";
2767*67e74705SXin Li }
2768*67e74705SXin Li
GenerateLangOptRequirements(const Record & R,raw_ostream & OS)2769*67e74705SXin Li static std::string GenerateLangOptRequirements(const Record &R,
2770*67e74705SXin Li raw_ostream &OS) {
2771*67e74705SXin Li // If the attribute has an empty or unset list of language requirements,
2772*67e74705SXin Li // return the default handler.
2773*67e74705SXin Li std::vector<Record *> LangOpts = R.getValueAsListOfDefs("LangOpts");
2774*67e74705SXin Li if (LangOpts.empty())
2775*67e74705SXin Li return "defaultDiagnoseLangOpts";
2776*67e74705SXin Li
2777*67e74705SXin Li // Generate the test condition, as well as a unique function name for the
2778*67e74705SXin Li // diagnostic test. The list of options should usually be short (one or two
2779*67e74705SXin Li // options), and the uniqueness isn't strictly necessary (it is just for
2780*67e74705SXin Li // codegen efficiency).
2781*67e74705SXin Li std::string FnName = "check", Test;
2782*67e74705SXin Li for (auto I = LangOpts.begin(), E = LangOpts.end(); I != E; ++I) {
2783*67e74705SXin Li std::string Part = (*I)->getValueAsString("Name");
2784*67e74705SXin Li if ((*I)->getValueAsBit("Negated"))
2785*67e74705SXin Li Test += "!";
2786*67e74705SXin Li Test += "S.LangOpts." + Part;
2787*67e74705SXin Li if (I + 1 != E)
2788*67e74705SXin Li Test += " || ";
2789*67e74705SXin Li FnName += Part;
2790*67e74705SXin Li }
2791*67e74705SXin Li FnName += "LangOpts";
2792*67e74705SXin Li
2793*67e74705SXin Li // If this code has already been generated, simply return the previous
2794*67e74705SXin Li // instance of it.
2795*67e74705SXin Li static std::set<std::string> CustomLangOptsSet;
2796*67e74705SXin Li auto I = CustomLangOptsSet.find(FnName);
2797*67e74705SXin Li if (I != CustomLangOptsSet.end())
2798*67e74705SXin Li return *I;
2799*67e74705SXin Li
2800*67e74705SXin Li OS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr) {\n";
2801*67e74705SXin Li OS << " if (" << Test << ")\n";
2802*67e74705SXin Li OS << " return true;\n\n";
2803*67e74705SXin Li OS << " S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) ";
2804*67e74705SXin Li OS << "<< Attr.getName();\n";
2805*67e74705SXin Li OS << " return false;\n";
2806*67e74705SXin Li OS << "}\n\n";
2807*67e74705SXin Li
2808*67e74705SXin Li CustomLangOptsSet.insert(FnName);
2809*67e74705SXin Li return FnName;
2810*67e74705SXin Li }
2811*67e74705SXin Li
GenerateDefaultTargetRequirements(raw_ostream & OS)2812*67e74705SXin Li static void GenerateDefaultTargetRequirements(raw_ostream &OS) {
2813*67e74705SXin Li OS << "static bool defaultTargetRequirements(const TargetInfo &) {\n";
2814*67e74705SXin Li OS << " return true;\n";
2815*67e74705SXin Li OS << "}\n\n";
2816*67e74705SXin Li }
2817*67e74705SXin Li
GenerateTargetRequirements(const Record & Attr,const ParsedAttrMap & Dupes,raw_ostream & OS)2818*67e74705SXin Li static std::string GenerateTargetRequirements(const Record &Attr,
2819*67e74705SXin Li const ParsedAttrMap &Dupes,
2820*67e74705SXin Li raw_ostream &OS) {
2821*67e74705SXin Li // If the attribute is not a target specific attribute, return the default
2822*67e74705SXin Li // target handler.
2823*67e74705SXin Li if (!Attr.isSubClassOf("TargetSpecificAttr"))
2824*67e74705SXin Li return "defaultTargetRequirements";
2825*67e74705SXin Li
2826*67e74705SXin Li // Get the list of architectures to be tested for.
2827*67e74705SXin Li const Record *R = Attr.getValueAsDef("Target");
2828*67e74705SXin Li std::vector<std::string> Arches = R->getValueAsListOfStrings("Arches");
2829*67e74705SXin Li if (Arches.empty()) {
2830*67e74705SXin Li PrintError(Attr.getLoc(), "Empty list of target architectures for a "
2831*67e74705SXin Li "target-specific attr");
2832*67e74705SXin Li return "defaultTargetRequirements";
2833*67e74705SXin Li }
2834*67e74705SXin Li
2835*67e74705SXin Li // If there are other attributes which share the same parsed attribute kind,
2836*67e74705SXin Li // such as target-specific attributes with a shared spelling, collapse the
2837*67e74705SXin Li // duplicate architectures. This is required because a shared target-specific
2838*67e74705SXin Li // attribute has only one AttributeList::Kind enumeration value, but it
2839*67e74705SXin Li // applies to multiple target architectures. In order for the attribute to be
2840*67e74705SXin Li // considered valid, all of its architectures need to be included.
2841*67e74705SXin Li if (!Attr.isValueUnset("ParseKind")) {
2842*67e74705SXin Li std::string APK = Attr.getValueAsString("ParseKind");
2843*67e74705SXin Li for (const auto &I : Dupes) {
2844*67e74705SXin Li if (I.first == APK) {
2845*67e74705SXin Li std::vector<std::string> DA = I.second->getValueAsDef("Target")
2846*67e74705SXin Li ->getValueAsListOfStrings("Arches");
2847*67e74705SXin Li std::copy(DA.begin(), DA.end(), std::back_inserter(Arches));
2848*67e74705SXin Li }
2849*67e74705SXin Li }
2850*67e74705SXin Li }
2851*67e74705SXin Li
2852*67e74705SXin Li std::string FnName = "isTarget";
2853*67e74705SXin Li std::string Test;
2854*67e74705SXin Li GenerateTargetSpecificAttrChecks(R, Arches, Test, &FnName);
2855*67e74705SXin Li
2856*67e74705SXin Li // If this code has already been generated, simply return the previous
2857*67e74705SXin Li // instance of it.
2858*67e74705SXin Li static std::set<std::string> CustomTargetSet;
2859*67e74705SXin Li auto I = CustomTargetSet.find(FnName);
2860*67e74705SXin Li if (I != CustomTargetSet.end())
2861*67e74705SXin Li return *I;
2862*67e74705SXin Li
2863*67e74705SXin Li OS << "static bool " << FnName << "(const TargetInfo &Target) {\n";
2864*67e74705SXin Li OS << " const llvm::Triple &T = Target.getTriple();\n";
2865*67e74705SXin Li OS << " return " << Test << ";\n";
2866*67e74705SXin Li OS << "}\n\n";
2867*67e74705SXin Li
2868*67e74705SXin Li CustomTargetSet.insert(FnName);
2869*67e74705SXin Li return FnName;
2870*67e74705SXin Li }
2871*67e74705SXin Li
GenerateDefaultSpellingIndexToSemanticSpelling(raw_ostream & OS)2872*67e74705SXin Li static void GenerateDefaultSpellingIndexToSemanticSpelling(raw_ostream &OS) {
2873*67e74705SXin Li OS << "static unsigned defaultSpellingIndexToSemanticSpelling("
2874*67e74705SXin Li << "const AttributeList &Attr) {\n";
2875*67e74705SXin Li OS << " return UINT_MAX;\n";
2876*67e74705SXin Li OS << "}\n\n";
2877*67e74705SXin Li }
2878*67e74705SXin Li
GenerateSpellingIndexToSemanticSpelling(const Record & Attr,raw_ostream & OS)2879*67e74705SXin Li static std::string GenerateSpellingIndexToSemanticSpelling(const Record &Attr,
2880*67e74705SXin Li raw_ostream &OS) {
2881*67e74705SXin Li // If the attribute does not have a semantic form, we can bail out early.
2882*67e74705SXin Li if (!Attr.getValueAsBit("ASTNode"))
2883*67e74705SXin Li return "defaultSpellingIndexToSemanticSpelling";
2884*67e74705SXin Li
2885*67e74705SXin Li std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
2886*67e74705SXin Li
2887*67e74705SXin Li // If there are zero or one spellings, or all of the spellings share the same
2888*67e74705SXin Li // name, we can also bail out early.
2889*67e74705SXin Li if (Spellings.size() <= 1 || SpellingNamesAreCommon(Spellings))
2890*67e74705SXin Li return "defaultSpellingIndexToSemanticSpelling";
2891*67e74705SXin Li
2892*67e74705SXin Li // Generate the enumeration we will use for the mapping.
2893*67e74705SXin Li SemanticSpellingMap SemanticToSyntacticMap;
2894*67e74705SXin Li std::string Enum = CreateSemanticSpellings(Spellings, SemanticToSyntacticMap);
2895*67e74705SXin Li std::string Name = Attr.getName() + "AttrSpellingMap";
2896*67e74705SXin Li
2897*67e74705SXin Li OS << "static unsigned " << Name << "(const AttributeList &Attr) {\n";
2898*67e74705SXin Li OS << Enum;
2899*67e74705SXin Li OS << " unsigned Idx = Attr.getAttributeSpellingListIndex();\n";
2900*67e74705SXin Li WriteSemanticSpellingSwitch("Idx", SemanticToSyntacticMap, OS);
2901*67e74705SXin Li OS << "}\n\n";
2902*67e74705SXin Li
2903*67e74705SXin Li return Name;
2904*67e74705SXin Li }
2905*67e74705SXin Li
IsKnownToGCC(const Record & Attr)2906*67e74705SXin Li static bool IsKnownToGCC(const Record &Attr) {
2907*67e74705SXin Li // Look at the spellings for this subject; if there are any spellings which
2908*67e74705SXin Li // claim to be known to GCC, the attribute is known to GCC.
2909*67e74705SXin Li std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
2910*67e74705SXin Li for (const auto &I : Spellings) {
2911*67e74705SXin Li if (I.knownToGCC())
2912*67e74705SXin Li return true;
2913*67e74705SXin Li }
2914*67e74705SXin Li return false;
2915*67e74705SXin Li }
2916*67e74705SXin Li
2917*67e74705SXin Li /// Emits the parsed attribute helpers
EmitClangAttrParsedAttrImpl(RecordKeeper & Records,raw_ostream & OS)2918*67e74705SXin Li void EmitClangAttrParsedAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
2919*67e74705SXin Li emitSourceFileHeader("Parsed attribute helpers", OS);
2920*67e74705SXin Li
2921*67e74705SXin Li // Get the list of parsed attributes, and accept the optional list of
2922*67e74705SXin Li // duplicates due to the ParseKind.
2923*67e74705SXin Li ParsedAttrMap Dupes;
2924*67e74705SXin Li ParsedAttrMap Attrs = getParsedAttrList(Records, &Dupes);
2925*67e74705SXin Li
2926*67e74705SXin Li // Generate the default appertainsTo, target and language option diagnostic,
2927*67e74705SXin Li // and spelling list index mapping methods.
2928*67e74705SXin Li GenerateDefaultAppertainsTo(OS);
2929*67e74705SXin Li GenerateDefaultLangOptRequirements(OS);
2930*67e74705SXin Li GenerateDefaultTargetRequirements(OS);
2931*67e74705SXin Li GenerateDefaultSpellingIndexToSemanticSpelling(OS);
2932*67e74705SXin Li
2933*67e74705SXin Li // Generate the appertainsTo diagnostic methods and write their names into
2934*67e74705SXin Li // another mapping. At the same time, generate the AttrInfoMap object
2935*67e74705SXin Li // contents. Due to the reliance on generated code, use separate streams so
2936*67e74705SXin Li // that code will not be interleaved.
2937*67e74705SXin Li std::stringstream SS;
2938*67e74705SXin Li for (auto I = Attrs.begin(), E = Attrs.end(); I != E; ++I) {
2939*67e74705SXin Li // TODO: If the attribute's kind appears in the list of duplicates, that is
2940*67e74705SXin Li // because it is a target-specific attribute that appears multiple times.
2941*67e74705SXin Li // It would be beneficial to test whether the duplicates are "similar
2942*67e74705SXin Li // enough" to each other to not cause problems. For instance, check that
2943*67e74705SXin Li // the spellings are identical, and custom parsing rules match, etc.
2944*67e74705SXin Li
2945*67e74705SXin Li // We need to generate struct instances based off ParsedAttrInfo from
2946*67e74705SXin Li // AttributeList.cpp.
2947*67e74705SXin Li SS << " { ";
2948*67e74705SXin Li emitArgInfo(*I->second, SS);
2949*67e74705SXin Li SS << ", " << I->second->getValueAsBit("HasCustomParsing");
2950*67e74705SXin Li SS << ", " << I->second->isSubClassOf("TargetSpecificAttr");
2951*67e74705SXin Li SS << ", " << I->second->isSubClassOf("TypeAttr");
2952*67e74705SXin Li SS << ", " << I->second->isSubClassOf("StmtAttr");
2953*67e74705SXin Li SS << ", " << IsKnownToGCC(*I->second);
2954*67e74705SXin Li SS << ", " << GenerateAppertainsTo(*I->second, OS);
2955*67e74705SXin Li SS << ", " << GenerateLangOptRequirements(*I->second, OS);
2956*67e74705SXin Li SS << ", " << GenerateTargetRequirements(*I->second, Dupes, OS);
2957*67e74705SXin Li SS << ", " << GenerateSpellingIndexToSemanticSpelling(*I->second, OS);
2958*67e74705SXin Li SS << " }";
2959*67e74705SXin Li
2960*67e74705SXin Li if (I + 1 != E)
2961*67e74705SXin Li SS << ",";
2962*67e74705SXin Li
2963*67e74705SXin Li SS << " // AT_" << I->first << "\n";
2964*67e74705SXin Li }
2965*67e74705SXin Li
2966*67e74705SXin Li OS << "static const ParsedAttrInfo AttrInfoMap[AttributeList::UnknownAttribute + 1] = {\n";
2967*67e74705SXin Li OS << SS.str();
2968*67e74705SXin Li OS << "};\n\n";
2969*67e74705SXin Li }
2970*67e74705SXin Li
2971*67e74705SXin Li // Emits the kind list of parsed attributes
EmitClangAttrParsedAttrKinds(RecordKeeper & Records,raw_ostream & OS)2972*67e74705SXin Li void EmitClangAttrParsedAttrKinds(RecordKeeper &Records, raw_ostream &OS) {
2973*67e74705SXin Li emitSourceFileHeader("Attribute name matcher", OS);
2974*67e74705SXin Li
2975*67e74705SXin Li std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
2976*67e74705SXin Li std::vector<StringMatcher::StringPair> GNU, Declspec, CXX11, Keywords, Pragma;
2977*67e74705SXin Li std::set<std::string> Seen;
2978*67e74705SXin Li for (const auto *A : Attrs) {
2979*67e74705SXin Li const Record &Attr = *A;
2980*67e74705SXin Li
2981*67e74705SXin Li bool SemaHandler = Attr.getValueAsBit("SemaHandler");
2982*67e74705SXin Li bool Ignored = Attr.getValueAsBit("Ignored");
2983*67e74705SXin Li if (SemaHandler || Ignored) {
2984*67e74705SXin Li // Attribute spellings can be shared between target-specific attributes,
2985*67e74705SXin Li // and can be shared between syntaxes for the same attribute. For
2986*67e74705SXin Li // instance, an attribute can be spelled GNU<"interrupt"> for an ARM-
2987*67e74705SXin Li // specific attribute, or MSP430-specific attribute. Additionally, an
2988*67e74705SXin Li // attribute can be spelled GNU<"dllexport"> and Declspec<"dllexport">
2989*67e74705SXin Li // for the same semantic attribute. Ultimately, we need to map each of
2990*67e74705SXin Li // these to a single AttributeList::Kind value, but the StringMatcher
2991*67e74705SXin Li // class cannot handle duplicate match strings. So we generate a list of
2992*67e74705SXin Li // string to match based on the syntax, and emit multiple string matchers
2993*67e74705SXin Li // depending on the syntax used.
2994*67e74705SXin Li std::string AttrName;
2995*67e74705SXin Li if (Attr.isSubClassOf("TargetSpecificAttr") &&
2996*67e74705SXin Li !Attr.isValueUnset("ParseKind")) {
2997*67e74705SXin Li AttrName = Attr.getValueAsString("ParseKind");
2998*67e74705SXin Li if (Seen.find(AttrName) != Seen.end())
2999*67e74705SXin Li continue;
3000*67e74705SXin Li Seen.insert(AttrName);
3001*67e74705SXin Li } else
3002*67e74705SXin Li AttrName = NormalizeAttrName(StringRef(Attr.getName())).str();
3003*67e74705SXin Li
3004*67e74705SXin Li std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
3005*67e74705SXin Li for (const auto &S : Spellings) {
3006*67e74705SXin Li const std::string &RawSpelling = S.name();
3007*67e74705SXin Li std::vector<StringMatcher::StringPair> *Matches = nullptr;
3008*67e74705SXin Li std::string Spelling;
3009*67e74705SXin Li const std::string &Variety = S.variety();
3010*67e74705SXin Li if (Variety == "CXX11") {
3011*67e74705SXin Li Matches = &CXX11;
3012*67e74705SXin Li Spelling += S.nameSpace();
3013*67e74705SXin Li Spelling += "::";
3014*67e74705SXin Li } else if (Variety == "GNU")
3015*67e74705SXin Li Matches = &GNU;
3016*67e74705SXin Li else if (Variety == "Declspec")
3017*67e74705SXin Li Matches = &Declspec;
3018*67e74705SXin Li else if (Variety == "Keyword")
3019*67e74705SXin Li Matches = &Keywords;
3020*67e74705SXin Li else if (Variety == "Pragma")
3021*67e74705SXin Li Matches = &Pragma;
3022*67e74705SXin Li
3023*67e74705SXin Li assert(Matches && "Unsupported spelling variety found");
3024*67e74705SXin Li
3025*67e74705SXin Li Spelling += NormalizeAttrSpelling(RawSpelling);
3026*67e74705SXin Li if (SemaHandler)
3027*67e74705SXin Li Matches->push_back(StringMatcher::StringPair(Spelling,
3028*67e74705SXin Li "return AttributeList::AT_" + AttrName + ";"));
3029*67e74705SXin Li else
3030*67e74705SXin Li Matches->push_back(StringMatcher::StringPair(Spelling,
3031*67e74705SXin Li "return AttributeList::IgnoredAttribute;"));
3032*67e74705SXin Li }
3033*67e74705SXin Li }
3034*67e74705SXin Li }
3035*67e74705SXin Li
3036*67e74705SXin Li OS << "static AttributeList::Kind getAttrKind(StringRef Name, ";
3037*67e74705SXin Li OS << "AttributeList::Syntax Syntax) {\n";
3038*67e74705SXin Li OS << " if (AttributeList::AS_GNU == Syntax) {\n";
3039*67e74705SXin Li StringMatcher("Name", GNU, OS).Emit();
3040*67e74705SXin Li OS << " } else if (AttributeList::AS_Declspec == Syntax) {\n";
3041*67e74705SXin Li StringMatcher("Name", Declspec, OS).Emit();
3042*67e74705SXin Li OS << " } else if (AttributeList::AS_CXX11 == Syntax) {\n";
3043*67e74705SXin Li StringMatcher("Name", CXX11, OS).Emit();
3044*67e74705SXin Li OS << " } else if (AttributeList::AS_Keyword == Syntax || ";
3045*67e74705SXin Li OS << "AttributeList::AS_ContextSensitiveKeyword == Syntax) {\n";
3046*67e74705SXin Li StringMatcher("Name", Keywords, OS).Emit();
3047*67e74705SXin Li OS << " } else if (AttributeList::AS_Pragma == Syntax) {\n";
3048*67e74705SXin Li StringMatcher("Name", Pragma, OS).Emit();
3049*67e74705SXin Li OS << " }\n";
3050*67e74705SXin Li OS << " return AttributeList::UnknownAttribute;\n"
3051*67e74705SXin Li << "}\n";
3052*67e74705SXin Li }
3053*67e74705SXin Li
3054*67e74705SXin Li // Emits the code to dump an attribute.
EmitClangAttrDump(RecordKeeper & Records,raw_ostream & OS)3055*67e74705SXin Li void EmitClangAttrDump(RecordKeeper &Records, raw_ostream &OS) {
3056*67e74705SXin Li emitSourceFileHeader("Attribute dumper", OS);
3057*67e74705SXin Li
3058*67e74705SXin Li OS << " switch (A->getKind()) {\n";
3059*67e74705SXin Li std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
3060*67e74705SXin Li for (const auto *Attr : Attrs) {
3061*67e74705SXin Li const Record &R = *Attr;
3062*67e74705SXin Li if (!R.getValueAsBit("ASTNode"))
3063*67e74705SXin Li continue;
3064*67e74705SXin Li OS << " case attr::" << R.getName() << ": {\n";
3065*67e74705SXin Li
3066*67e74705SXin Li // If the attribute has a semantically-meaningful name (which is determined
3067*67e74705SXin Li // by whether there is a Spelling enumeration for it), then write out the
3068*67e74705SXin Li // spelling used for the attribute.
3069*67e74705SXin Li std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
3070*67e74705SXin Li if (Spellings.size() > 1 && !SpellingNamesAreCommon(Spellings))
3071*67e74705SXin Li OS << " OS << \" \" << A->getSpelling();\n";
3072*67e74705SXin Li
3073*67e74705SXin Li Args = R.getValueAsListOfDefs("Args");
3074*67e74705SXin Li if (!Args.empty()) {
3075*67e74705SXin Li OS << " const auto *SA = cast<" << R.getName()
3076*67e74705SXin Li << "Attr>(A);\n";
3077*67e74705SXin Li for (const auto *Arg : Args)
3078*67e74705SXin Li createArgument(*Arg, R.getName())->writeDump(OS);
3079*67e74705SXin Li
3080*67e74705SXin Li for (const auto *AI : Args)
3081*67e74705SXin Li createArgument(*AI, R.getName())->writeDumpChildren(OS);
3082*67e74705SXin Li }
3083*67e74705SXin Li OS <<
3084*67e74705SXin Li " break;\n"
3085*67e74705SXin Li " }\n";
3086*67e74705SXin Li }
3087*67e74705SXin Li OS << " }\n";
3088*67e74705SXin Li }
3089*67e74705SXin Li
EmitClangAttrParserStringSwitches(RecordKeeper & Records,raw_ostream & OS)3090*67e74705SXin Li void EmitClangAttrParserStringSwitches(RecordKeeper &Records,
3091*67e74705SXin Li raw_ostream &OS) {
3092*67e74705SXin Li emitSourceFileHeader("Parser-related llvm::StringSwitch cases", OS);
3093*67e74705SXin Li emitClangAttrArgContextList(Records, OS);
3094*67e74705SXin Li emitClangAttrIdentifierArgList(Records, OS);
3095*67e74705SXin Li emitClangAttrTypeArgList(Records, OS);
3096*67e74705SXin Li emitClangAttrLateParsedList(Records, OS);
3097*67e74705SXin Li }
3098*67e74705SXin Li
3099*67e74705SXin Li class DocumentationData {
3100*67e74705SXin Li public:
3101*67e74705SXin Li const Record *Documentation;
3102*67e74705SXin Li const Record *Attribute;
3103*67e74705SXin Li
DocumentationData(const Record & Documentation,const Record & Attribute)3104*67e74705SXin Li DocumentationData(const Record &Documentation, const Record &Attribute)
3105*67e74705SXin Li : Documentation(&Documentation), Attribute(&Attribute) {}
3106*67e74705SXin Li };
3107*67e74705SXin Li
WriteCategoryHeader(const Record * DocCategory,raw_ostream & OS)3108*67e74705SXin Li static void WriteCategoryHeader(const Record *DocCategory,
3109*67e74705SXin Li raw_ostream &OS) {
3110*67e74705SXin Li const std::string &Name = DocCategory->getValueAsString("Name");
3111*67e74705SXin Li OS << Name << "\n" << std::string(Name.length(), '=') << "\n";
3112*67e74705SXin Li
3113*67e74705SXin Li // If there is content, print that as well.
3114*67e74705SXin Li std::string ContentStr = DocCategory->getValueAsString("Content");
3115*67e74705SXin Li // Trim leading and trailing newlines and spaces.
3116*67e74705SXin Li OS << StringRef(ContentStr).trim();
3117*67e74705SXin Li
3118*67e74705SXin Li OS << "\n\n";
3119*67e74705SXin Li }
3120*67e74705SXin Li
3121*67e74705SXin Li enum SpellingKind {
3122*67e74705SXin Li GNU = 1 << 0,
3123*67e74705SXin Li CXX11 = 1 << 1,
3124*67e74705SXin Li Declspec = 1 << 2,
3125*67e74705SXin Li Keyword = 1 << 3,
3126*67e74705SXin Li Pragma = 1 << 4
3127*67e74705SXin Li };
3128*67e74705SXin Li
WriteDocumentation(const DocumentationData & Doc,raw_ostream & OS)3129*67e74705SXin Li static void WriteDocumentation(const DocumentationData &Doc,
3130*67e74705SXin Li raw_ostream &OS) {
3131*67e74705SXin Li // FIXME: there is no way to have a per-spelling category for the attribute
3132*67e74705SXin Li // documentation. This may not be a limiting factor since the spellings
3133*67e74705SXin Li // should generally be consistently applied across the category.
3134*67e74705SXin Li
3135*67e74705SXin Li std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Doc.Attribute);
3136*67e74705SXin Li
3137*67e74705SXin Li // Determine the heading to be used for this attribute.
3138*67e74705SXin Li std::string Heading = Doc.Documentation->getValueAsString("Heading");
3139*67e74705SXin Li bool CustomHeading = !Heading.empty();
3140*67e74705SXin Li if (Heading.empty()) {
3141*67e74705SXin Li // If there's only one spelling, we can simply use that.
3142*67e74705SXin Li if (Spellings.size() == 1)
3143*67e74705SXin Li Heading = Spellings.begin()->name();
3144*67e74705SXin Li else {
3145*67e74705SXin Li std::set<std::string> Uniques;
3146*67e74705SXin Li for (auto I = Spellings.begin(), E = Spellings.end();
3147*67e74705SXin Li I != E && Uniques.size() <= 1; ++I) {
3148*67e74705SXin Li std::string Spelling = NormalizeNameForSpellingComparison(I->name());
3149*67e74705SXin Li Uniques.insert(Spelling);
3150*67e74705SXin Li }
3151*67e74705SXin Li // If the semantic map has only one spelling, that is sufficient for our
3152*67e74705SXin Li // needs.
3153*67e74705SXin Li if (Uniques.size() == 1)
3154*67e74705SXin Li Heading = *Uniques.begin();
3155*67e74705SXin Li }
3156*67e74705SXin Li }
3157*67e74705SXin Li
3158*67e74705SXin Li // If the heading is still empty, it is an error.
3159*67e74705SXin Li if (Heading.empty())
3160*67e74705SXin Li PrintFatalError(Doc.Attribute->getLoc(),
3161*67e74705SXin Li "This attribute requires a heading to be specified");
3162*67e74705SXin Li
3163*67e74705SXin Li // Gather a list of unique spellings; this is not the same as the semantic
3164*67e74705SXin Li // spelling for the attribute. Variations in underscores and other non-
3165*67e74705SXin Li // semantic characters are still acceptable.
3166*67e74705SXin Li std::vector<std::string> Names;
3167*67e74705SXin Li
3168*67e74705SXin Li unsigned SupportedSpellings = 0;
3169*67e74705SXin Li for (const auto &I : Spellings) {
3170*67e74705SXin Li SpellingKind Kind = StringSwitch<SpellingKind>(I.variety())
3171*67e74705SXin Li .Case("GNU", GNU)
3172*67e74705SXin Li .Case("CXX11", CXX11)
3173*67e74705SXin Li .Case("Declspec", Declspec)
3174*67e74705SXin Li .Case("Keyword", Keyword)
3175*67e74705SXin Li .Case("Pragma", Pragma);
3176*67e74705SXin Li
3177*67e74705SXin Li // Mask in the supported spelling.
3178*67e74705SXin Li SupportedSpellings |= Kind;
3179*67e74705SXin Li
3180*67e74705SXin Li std::string Name;
3181*67e74705SXin Li if (Kind == CXX11 && !I.nameSpace().empty())
3182*67e74705SXin Li Name = I.nameSpace() + "::";
3183*67e74705SXin Li Name += I.name();
3184*67e74705SXin Li
3185*67e74705SXin Li // If this name is the same as the heading, do not add it.
3186*67e74705SXin Li if (Name != Heading)
3187*67e74705SXin Li Names.push_back(Name);
3188*67e74705SXin Li }
3189*67e74705SXin Li
3190*67e74705SXin Li // Print out the heading for the attribute. If there are alternate spellings,
3191*67e74705SXin Li // then display those after the heading.
3192*67e74705SXin Li if (!CustomHeading && !Names.empty()) {
3193*67e74705SXin Li Heading += " (";
3194*67e74705SXin Li for (auto I = Names.begin(), E = Names.end(); I != E; ++I) {
3195*67e74705SXin Li if (I != Names.begin())
3196*67e74705SXin Li Heading += ", ";
3197*67e74705SXin Li Heading += *I;
3198*67e74705SXin Li }
3199*67e74705SXin Li Heading += ")";
3200*67e74705SXin Li }
3201*67e74705SXin Li OS << Heading << "\n" << std::string(Heading.length(), '-') << "\n";
3202*67e74705SXin Li
3203*67e74705SXin Li if (!SupportedSpellings)
3204*67e74705SXin Li PrintFatalError(Doc.Attribute->getLoc(),
3205*67e74705SXin Li "Attribute has no supported spellings; cannot be "
3206*67e74705SXin Li "documented");
3207*67e74705SXin Li
3208*67e74705SXin Li // List what spelling syntaxes the attribute supports.
3209*67e74705SXin Li OS << ".. csv-table:: Supported Syntaxes\n";
3210*67e74705SXin Li OS << " :header: \"GNU\", \"C++11\", \"__declspec\", \"Keyword\",";
3211*67e74705SXin Li OS << " \"Pragma\"\n\n";
3212*67e74705SXin Li OS << " \"";
3213*67e74705SXin Li if (SupportedSpellings & GNU) OS << "X";
3214*67e74705SXin Li OS << "\",\"";
3215*67e74705SXin Li if (SupportedSpellings & CXX11) OS << "X";
3216*67e74705SXin Li OS << "\",\"";
3217*67e74705SXin Li if (SupportedSpellings & Declspec) OS << "X";
3218*67e74705SXin Li OS << "\",\"";
3219*67e74705SXin Li if (SupportedSpellings & Keyword) OS << "X";
3220*67e74705SXin Li OS << "\", \"";
3221*67e74705SXin Li if (SupportedSpellings & Pragma) OS << "X";
3222*67e74705SXin Li OS << "\"\n\n";
3223*67e74705SXin Li
3224*67e74705SXin Li // If the attribute is deprecated, print a message about it, and possibly
3225*67e74705SXin Li // provide a replacement attribute.
3226*67e74705SXin Li if (!Doc.Documentation->isValueUnset("Deprecated")) {
3227*67e74705SXin Li OS << "This attribute has been deprecated, and may be removed in a future "
3228*67e74705SXin Li << "version of Clang.";
3229*67e74705SXin Li const Record &Deprecated = *Doc.Documentation->getValueAsDef("Deprecated");
3230*67e74705SXin Li std::string Replacement = Deprecated.getValueAsString("Replacement");
3231*67e74705SXin Li if (!Replacement.empty())
3232*67e74705SXin Li OS << " This attribute has been superseded by ``"
3233*67e74705SXin Li << Replacement << "``.";
3234*67e74705SXin Li OS << "\n\n";
3235*67e74705SXin Li }
3236*67e74705SXin Li
3237*67e74705SXin Li std::string ContentStr = Doc.Documentation->getValueAsString("Content");
3238*67e74705SXin Li // Trim leading and trailing newlines and spaces.
3239*67e74705SXin Li OS << StringRef(ContentStr).trim();
3240*67e74705SXin Li
3241*67e74705SXin Li OS << "\n\n\n";
3242*67e74705SXin Li }
3243*67e74705SXin Li
EmitClangAttrDocs(RecordKeeper & Records,raw_ostream & OS)3244*67e74705SXin Li void EmitClangAttrDocs(RecordKeeper &Records, raw_ostream &OS) {
3245*67e74705SXin Li // Get the documentation introduction paragraph.
3246*67e74705SXin Li const Record *Documentation = Records.getDef("GlobalDocumentation");
3247*67e74705SXin Li if (!Documentation) {
3248*67e74705SXin Li PrintFatalError("The Documentation top-level definition is missing, "
3249*67e74705SXin Li "no documentation will be generated.");
3250*67e74705SXin Li return;
3251*67e74705SXin Li }
3252*67e74705SXin Li
3253*67e74705SXin Li OS << Documentation->getValueAsString("Intro") << "\n";
3254*67e74705SXin Li
3255*67e74705SXin Li // Gather the Documentation lists from each of the attributes, based on the
3256*67e74705SXin Li // category provided.
3257*67e74705SXin Li std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
3258*67e74705SXin Li std::map<const Record *, std::vector<DocumentationData>> SplitDocs;
3259*67e74705SXin Li for (const auto *A : Attrs) {
3260*67e74705SXin Li const Record &Attr = *A;
3261*67e74705SXin Li std::vector<Record *> Docs = Attr.getValueAsListOfDefs("Documentation");
3262*67e74705SXin Li for (const auto *D : Docs) {
3263*67e74705SXin Li const Record &Doc = *D;
3264*67e74705SXin Li const Record *Category = Doc.getValueAsDef("Category");
3265*67e74705SXin Li // If the category is "undocumented", then there cannot be any other
3266*67e74705SXin Li // documentation categories (otherwise, the attribute would become
3267*67e74705SXin Li // documented).
3268*67e74705SXin Li std::string Cat = Category->getValueAsString("Name");
3269*67e74705SXin Li bool Undocumented = Cat == "Undocumented";
3270*67e74705SXin Li if (Undocumented && Docs.size() > 1)
3271*67e74705SXin Li PrintFatalError(Doc.getLoc(),
3272*67e74705SXin Li "Attribute is \"Undocumented\", but has multiple "
3273*67e74705SXin Li "documentation categories");
3274*67e74705SXin Li
3275*67e74705SXin Li if (!Undocumented)
3276*67e74705SXin Li SplitDocs[Category].push_back(DocumentationData(Doc, Attr));
3277*67e74705SXin Li }
3278*67e74705SXin Li }
3279*67e74705SXin Li
3280*67e74705SXin Li // Having split the attributes out based on what documentation goes where,
3281*67e74705SXin Li // we can begin to generate sections of documentation.
3282*67e74705SXin Li for (const auto &I : SplitDocs) {
3283*67e74705SXin Li WriteCategoryHeader(I.first, OS);
3284*67e74705SXin Li
3285*67e74705SXin Li // Walk over each of the attributes in the category and write out their
3286*67e74705SXin Li // documentation.
3287*67e74705SXin Li for (const auto &Doc : I.second)
3288*67e74705SXin Li WriteDocumentation(Doc, OS);
3289*67e74705SXin Li }
3290*67e74705SXin Li }
3291*67e74705SXin Li
3292*67e74705SXin Li } // end namespace clang
3293