Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1 | //===- ClangAttrEmitter.cpp - Generate Clang attribute handling =-*- C++ -*--=// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // These tablegen backends emit Clang attribute processing code |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/ArrayRef.h" |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/DenseMap.h" |
George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseSet.h" |
Alex Lorenz | 3bfe962 | 2017-04-18 10:46:41 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallString.h" |
Aaron Ballman | 28afa18 | 2014-11-17 18:17:19 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringExtras.h" |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringRef.h" |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringSet.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringSwitch.h" |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/iterator_range.h" |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 26 | #include "llvm/TableGen/Error.h" |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 27 | #include "llvm/TableGen/Record.h" |
Douglas Gregor | 377f99b | 2012-05-02 17:33:51 +0000 | [diff] [blame] | 28 | #include "llvm/TableGen/StringMatcher.h" |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 29 | #include "llvm/TableGen/TableGenBackend.h" |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 30 | #include <algorithm> |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 31 | #include <cassert> |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 32 | #include <cctype> |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 33 | #include <cstddef> |
| 34 | #include <cstdint> |
| 35 | #include <map> |
Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 36 | #include <memory> |
Aaron Ballman | 8046903 | 2013-11-29 14:57:58 +0000 | [diff] [blame] | 37 | #include <set> |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 38 | #include <sstream> |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 39 | #include <string> |
| 40 | #include <utility> |
| 41 | #include <vector> |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 42 | |
| 43 | using namespace llvm; |
| 44 | |
Benjamin Kramer | d910d16 | 2015-03-10 18:24:01 +0000 | [diff] [blame] | 45 | namespace { |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 46 | |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 47 | class FlattenedSpelling { |
| 48 | std::string V, N, NS; |
| 49 | bool K; |
| 50 | |
| 51 | public: |
| 52 | FlattenedSpelling(const std::string &Variety, const std::string &Name, |
| 53 | const std::string &Namespace, bool KnownToGCC) : |
| 54 | V(Variety), N(Name), NS(Namespace), K(KnownToGCC) {} |
| 55 | explicit FlattenedSpelling(const Record &Spelling) : |
| 56 | V(Spelling.getValueAsString("Variety")), |
| 57 | N(Spelling.getValueAsString("Name")) { |
| 58 | |
| 59 | assert(V != "GCC" && "Given a GCC spelling, which means this hasn't been" |
| 60 | "flattened!"); |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame^] | 61 | if (V == "CXX11" || V == "C2x" || V == "Pragma") |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 62 | NS = Spelling.getValueAsString("Namespace"); |
| 63 | bool Unset; |
| 64 | K = Spelling.getValueAsBitOrUnset("KnownToGCC", Unset); |
| 65 | } |
| 66 | |
| 67 | const std::string &variety() const { return V; } |
| 68 | const std::string &name() const { return N; } |
| 69 | const std::string &nameSpace() const { return NS; } |
| 70 | bool knownToGCC() const { return K; } |
| 71 | }; |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 72 | |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 73 | } // end anonymous namespace |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 74 | |
Benjamin Kramer | d910d16 | 2015-03-10 18:24:01 +0000 | [diff] [blame] | 75 | static std::vector<FlattenedSpelling> |
| 76 | GetFlattenedSpellings(const Record &Attr) { |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 77 | std::vector<Record *> Spellings = Attr.getValueAsListOfDefs("Spellings"); |
| 78 | std::vector<FlattenedSpelling> Ret; |
| 79 | |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 80 | for (const auto &Spelling : Spellings) { |
| 81 | if (Spelling->getValueAsString("Variety") == "GCC") { |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 82 | // Gin up two new spelling objects to add into the list. |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 83 | Ret.emplace_back("GNU", Spelling->getValueAsString("Name"), "", true); |
| 84 | Ret.emplace_back("CXX11", Spelling->getValueAsString("Name"), "gnu", |
| 85 | true); |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 86 | } else |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 87 | Ret.push_back(FlattenedSpelling(*Spelling)); |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | return Ret; |
| 91 | } |
| 92 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 93 | static std::string ReadPCHRecord(StringRef type) { |
| 94 | return StringSwitch<std::string>(type) |
David L. Jones | 267b884 | 2017-01-24 01:04:30 +0000 | [diff] [blame] | 95 | .EndsWith("Decl *", "Record.GetLocalDeclAs<" |
| 96 | + std::string(type, 0, type.size()-1) + ">(Record.readInt())") |
| 97 | .Case("TypeSourceInfo *", "Record.getTypeSourceInfo()") |
| 98 | .Case("Expr *", "Record.readExpr()") |
| 99 | .Case("IdentifierInfo *", "Record.getIdentifierInfo()") |
| 100 | .Case("StringRef", "Record.readString()") |
| 101 | .Default("Record.readInt()"); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Richard Smith | 1997856 | 2016-05-18 00:16:51 +0000 | [diff] [blame] | 104 | // Get a type that is suitable for storing an object of the specified type. |
| 105 | static StringRef getStorageType(StringRef type) { |
| 106 | return StringSwitch<StringRef>(type) |
| 107 | .Case("StringRef", "std::string") |
| 108 | .Default(type); |
| 109 | } |
| 110 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 111 | // Assumes that the way to get the value is SA->getname() |
| 112 | static std::string WritePCHRecord(StringRef type, StringRef name) { |
Richard Smith | 290d801 | 2016-04-06 17:06:00 +0000 | [diff] [blame] | 113 | return "Record." + StringSwitch<std::string>(type) |
| 114 | .EndsWith("Decl *", "AddDeclRef(" + std::string(name) + ");\n") |
| 115 | .Case("TypeSourceInfo *", "AddTypeSourceInfo(" + std::string(name) + ");\n") |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 116 | .Case("Expr *", "AddStmt(" + std::string(name) + ");\n") |
Richard Smith | 290d801 | 2016-04-06 17:06:00 +0000 | [diff] [blame] | 117 | .Case("IdentifierInfo *", "AddIdentifierRef(" + std::string(name) + ");\n") |
| 118 | .Case("StringRef", "AddString(" + std::string(name) + ");\n") |
| 119 | .Default("push_back(" + std::string(name) + ");\n"); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Michael Han | 4a04517 | 2012-03-07 00:12:16 +0000 | [diff] [blame] | 122 | // Normalize attribute name by removing leading and trailing |
| 123 | // underscores. For example, __foo, foo__, __foo__ would |
| 124 | // become foo. |
| 125 | static StringRef NormalizeAttrName(StringRef AttrName) { |
George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 126 | AttrName.consume_front("__"); |
| 127 | AttrName.consume_back("__"); |
Michael Han | 4a04517 | 2012-03-07 00:12:16 +0000 | [diff] [blame] | 128 | return AttrName; |
| 129 | } |
| 130 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 131 | // Normalize the name by removing any and all leading and trailing underscores. |
| 132 | // This is different from NormalizeAttrName in that it also handles names like |
| 133 | // _pascal and __pascal. |
| 134 | static StringRef NormalizeNameForSpellingComparison(StringRef Name) { |
Benjamin Kramer | 5c40407 | 2015-04-10 21:37:21 +0000 | [diff] [blame] | 135 | return Name.trim("_"); |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Justin Lebar | 4086fe5 | 2017-01-05 16:51:54 +0000 | [diff] [blame] | 138 | // Normalize the spelling of a GNU attribute (i.e. "x" in "__attribute__((x))"), |
| 139 | // removing "__" if it appears at the beginning and end of the attribute's name. |
| 140 | static StringRef NormalizeGNUAttrSpelling(StringRef AttrSpelling) { |
Michael Han | 4a04517 | 2012-03-07 00:12:16 +0000 | [diff] [blame] | 141 | if (AttrSpelling.startswith("__") && AttrSpelling.endswith("__")) { |
| 142 | AttrSpelling = AttrSpelling.substr(2, AttrSpelling.size() - 4); |
| 143 | } |
| 144 | |
| 145 | return AttrSpelling; |
| 146 | } |
| 147 | |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 148 | typedef std::vector<std::pair<std::string, const Record *>> ParsedAttrMap; |
Aaron Ballman | 64e6986 | 2013-12-15 13:05:48 +0000 | [diff] [blame] | 149 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 150 | static ParsedAttrMap getParsedAttrList(const RecordKeeper &Records, |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 151 | ParsedAttrMap *Dupes = nullptr) { |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 152 | std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); |
Aaron Ballman | 64e6986 | 2013-12-15 13:05:48 +0000 | [diff] [blame] | 153 | std::set<std::string> Seen; |
| 154 | ParsedAttrMap R; |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 155 | for (const auto *Attr : Attrs) { |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 156 | if (Attr->getValueAsBit("SemaHandler")) { |
Aaron Ballman | 64e6986 | 2013-12-15 13:05:48 +0000 | [diff] [blame] | 157 | std::string AN; |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 158 | if (Attr->isSubClassOf("TargetSpecificAttr") && |
| 159 | !Attr->isValueUnset("ParseKind")) { |
| 160 | AN = Attr->getValueAsString("ParseKind"); |
Aaron Ballman | 64e6986 | 2013-12-15 13:05:48 +0000 | [diff] [blame] | 161 | |
| 162 | // If this attribute has already been handled, it does not need to be |
| 163 | // handled again. |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 164 | if (Seen.find(AN) != Seen.end()) { |
| 165 | if (Dupes) |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 166 | Dupes->push_back(std::make_pair(AN, Attr)); |
Aaron Ballman | 64e6986 | 2013-12-15 13:05:48 +0000 | [diff] [blame] | 167 | continue; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 168 | } |
Aaron Ballman | 64e6986 | 2013-12-15 13:05:48 +0000 | [diff] [blame] | 169 | Seen.insert(AN); |
| 170 | } else |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 171 | AN = NormalizeAttrName(Attr->getName()).str(); |
Aaron Ballman | 64e6986 | 2013-12-15 13:05:48 +0000 | [diff] [blame] | 172 | |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 173 | R.push_back(std::make_pair(AN, Attr)); |
Aaron Ballman | 64e6986 | 2013-12-15 13:05:48 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | return R; |
| 177 | } |
| 178 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 179 | namespace { |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 180 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 181 | class Argument { |
| 182 | std::string lowerName, upperName; |
| 183 | StringRef attrName; |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 184 | bool isOpt; |
John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 185 | bool Fake; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 186 | |
| 187 | public: |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 188 | Argument(const Record &Arg, StringRef Attr) |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 189 | : lowerName(Arg.getValueAsString("Name")), upperName(lowerName), |
John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 190 | attrName(Attr), isOpt(false), Fake(false) { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 191 | if (!lowerName.empty()) { |
| 192 | lowerName[0] = std::tolower(lowerName[0]); |
| 193 | upperName[0] = std::toupper(upperName[0]); |
| 194 | } |
Reid Kleckner | ebeb0ca | 2016-05-31 17:42:56 +0000 | [diff] [blame] | 195 | // Work around MinGW's macro definition of 'interface' to 'struct'. We |
| 196 | // have an attribute argument called 'Interface', so only the lower case |
| 197 | // name conflicts with the macro definition. |
| 198 | if (lowerName == "interface") |
| 199 | lowerName = "interface_"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 200 | } |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 201 | virtual ~Argument() = default; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 202 | |
| 203 | StringRef getLowerName() const { return lowerName; } |
| 204 | StringRef getUpperName() const { return upperName; } |
| 205 | StringRef getAttrName() const { return attrName; } |
| 206 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 207 | bool isOptional() const { return isOpt; } |
| 208 | void setOptional(bool set) { isOpt = set; } |
| 209 | |
John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 210 | bool isFake() const { return Fake; } |
| 211 | void setFake(bool fake) { Fake = fake; } |
| 212 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 213 | // These functions print the argument contents formatted in different ways. |
| 214 | virtual void writeAccessors(raw_ostream &OS) const = 0; |
| 215 | virtual void writeAccessorDefinitions(raw_ostream &OS) const {} |
DeLesley Hutchins | c4a8243 | 2013-12-30 17:24:36 +0000 | [diff] [blame] | 216 | virtual void writeASTVisitorTraversal(raw_ostream &OS) const {} |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 217 | virtual void writeCloneArgs(raw_ostream &OS) const = 0; |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 218 | virtual void writeTemplateInstantiationArgs(raw_ostream &OS) const = 0; |
Daniel Dunbar | dc51baa | 2012-02-10 06:00:29 +0000 | [diff] [blame] | 219 | virtual void writeTemplateInstantiation(raw_ostream &OS) const {} |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 220 | virtual void writeCtorBody(raw_ostream &OS) const {} |
| 221 | virtual void writeCtorInitializers(raw_ostream &OS) const = 0; |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 222 | virtual void writeCtorDefaultInitializers(raw_ostream &OS) const = 0; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 223 | virtual void writeCtorParameters(raw_ostream &OS) const = 0; |
| 224 | virtual void writeDeclarations(raw_ostream &OS) const = 0; |
| 225 | virtual void writePCHReadArgs(raw_ostream &OS) const = 0; |
| 226 | virtual void writePCHReadDecls(raw_ostream &OS) const = 0; |
| 227 | virtual void writePCHWrite(raw_ostream &OS) const = 0; |
Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 228 | virtual void writeValue(raw_ostream &OS) const = 0; |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 229 | virtual void writeDump(raw_ostream &OS) const = 0; |
| 230 | virtual void writeDumpChildren(raw_ostream &OS) const {} |
Richard Trieu | de5cc7d | 2013-01-31 01:44:26 +0000 | [diff] [blame] | 231 | virtual void writeHasChildren(raw_ostream &OS) const { OS << "false"; } |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 232 | |
| 233 | virtual bool isEnumArg() const { return false; } |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 234 | virtual bool isVariadicEnumArg() const { return false; } |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 235 | virtual bool isVariadic() const { return false; } |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 236 | |
| 237 | virtual void writeImplicitCtorArgs(raw_ostream &OS) const { |
| 238 | OS << getUpperName(); |
| 239 | } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 240 | }; |
| 241 | |
| 242 | class SimpleArgument : public Argument { |
| 243 | std::string type; |
| 244 | |
| 245 | public: |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 246 | SimpleArgument(const Record &Arg, StringRef Attr, std::string T) |
Benjamin Kramer | cfeacf5 | 2016-05-27 14:27:13 +0000 | [diff] [blame] | 247 | : Argument(Arg, Attr), type(std::move(T)) {} |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 248 | |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 249 | std::string getType() const { return type; } |
| 250 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 251 | void writeAccessors(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 252 | OS << " " << type << " get" << getUpperName() << "() const {\n"; |
| 253 | OS << " return " << getLowerName() << ";\n"; |
| 254 | OS << " }"; |
| 255 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 256 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 257 | void writeCloneArgs(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 258 | OS << getLowerName(); |
| 259 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 260 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 261 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 262 | OS << "A->get" << getUpperName() << "()"; |
| 263 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 264 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 265 | void writeCtorInitializers(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 266 | OS << getLowerName() << "(" << getUpperName() << ")"; |
| 267 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 268 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 269 | void writeCtorDefaultInitializers(raw_ostream &OS) const override { |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 270 | OS << getLowerName() << "()"; |
| 271 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 272 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 273 | void writeCtorParameters(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 274 | OS << type << " " << getUpperName(); |
| 275 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 276 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 277 | void writeDeclarations(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 278 | OS << type << " " << getLowerName() << ";"; |
| 279 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 280 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 281 | void writePCHReadDecls(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 282 | std::string read = ReadPCHRecord(type); |
| 283 | OS << " " << type << " " << getLowerName() << " = " << read << ";\n"; |
| 284 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 285 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 286 | void writePCHReadArgs(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 287 | OS << getLowerName(); |
| 288 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 289 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 290 | void writePCHWrite(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 291 | OS << " " << WritePCHRecord(type, "SA->get" + |
| 292 | std::string(getUpperName()) + "()"); |
| 293 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 294 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 295 | void writeValue(raw_ostream &OS) const override { |
Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 296 | if (type == "FunctionDecl *") { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 297 | OS << "\" << get" << getUpperName() |
| 298 | << "()->getNameInfo().getAsString() << \""; |
Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 299 | } else if (type == "IdentifierInfo *") { |
Akira Hatanaka | 3d17313 | 2016-09-10 03:29:43 +0000 | [diff] [blame] | 300 | OS << "\";\n"; |
| 301 | if (isOptional()) |
| 302 | OS << " if (get" << getUpperName() << "()) "; |
| 303 | else |
| 304 | OS << " "; |
| 305 | OS << "OS << get" << getUpperName() << "()->getName();\n"; |
| 306 | OS << " OS << \""; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 307 | } else if (type == "TypeSourceInfo *") { |
Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 308 | OS << "\" << get" << getUpperName() << "().getAsString() << \""; |
Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 309 | } else { |
| 310 | OS << "\" << get" << getUpperName() << "() << \""; |
| 311 | } |
| 312 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 313 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 314 | void writeDump(raw_ostream &OS) const override { |
Argyrios Kyrtzidis | a7233bd | 2017-05-24 00:46:27 +0000 | [diff] [blame] | 315 | if (type == "FunctionDecl *" || type == "NamedDecl *") { |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 316 | OS << " OS << \" \";\n"; |
| 317 | OS << " dumpBareDeclRef(SA->get" << getUpperName() << "());\n"; |
| 318 | } else if (type == "IdentifierInfo *") { |
Aaron Ballman | 415c414 | 2015-11-30 15:25:34 +0000 | [diff] [blame] | 319 | if (isOptional()) |
| 320 | OS << " if (SA->get" << getUpperName() << "())\n "; |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 321 | OS << " OS << \" \" << SA->get" << getUpperName() |
| 322 | << "()->getName();\n"; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 323 | } else if (type == "TypeSourceInfo *") { |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 324 | OS << " OS << \" \" << SA->get" << getUpperName() |
| 325 | << "().getAsString();\n"; |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 326 | } else if (type == "bool") { |
| 327 | OS << " if (SA->get" << getUpperName() << "()) OS << \" " |
| 328 | << getUpperName() << "\";\n"; |
| 329 | } else if (type == "int" || type == "unsigned") { |
| 330 | OS << " OS << \" \" << SA->get" << getUpperName() << "();\n"; |
| 331 | } else { |
| 332 | llvm_unreachable("Unknown SimpleArgument type!"); |
| 333 | } |
| 334 | } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 335 | }; |
| 336 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 337 | class DefaultSimpleArgument : public SimpleArgument { |
| 338 | int64_t Default; |
| 339 | |
| 340 | public: |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 341 | DefaultSimpleArgument(const Record &Arg, StringRef Attr, |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 342 | std::string T, int64_t Default) |
| 343 | : SimpleArgument(Arg, Attr, T), Default(Default) {} |
| 344 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 345 | void writeAccessors(raw_ostream &OS) const override { |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 346 | SimpleArgument::writeAccessors(OS); |
| 347 | |
| 348 | OS << "\n\n static const " << getType() << " Default" << getUpperName() |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 349 | << " = "; |
| 350 | if (getType() == "bool") |
| 351 | OS << (Default != 0 ? "true" : "false"); |
| 352 | else |
| 353 | OS << Default; |
| 354 | OS << ";"; |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 355 | } |
| 356 | }; |
| 357 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 358 | class StringArgument : public Argument { |
| 359 | public: |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 360 | StringArgument(const Record &Arg, StringRef Attr) |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 361 | : Argument(Arg, Attr) |
| 362 | {} |
| 363 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 364 | void writeAccessors(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 365 | OS << " llvm::StringRef get" << getUpperName() << "() const {\n"; |
| 366 | OS << " return llvm::StringRef(" << getLowerName() << ", " |
| 367 | << getLowerName() << "Length);\n"; |
| 368 | OS << " }\n"; |
| 369 | OS << " unsigned get" << getUpperName() << "Length() const {\n"; |
| 370 | OS << " return " << getLowerName() << "Length;\n"; |
| 371 | OS << " }\n"; |
| 372 | OS << " void set" << getUpperName() |
| 373 | << "(ASTContext &C, llvm::StringRef S) {\n"; |
| 374 | OS << " " << getLowerName() << "Length = S.size();\n"; |
| 375 | OS << " this->" << getLowerName() << " = new (C, 1) char [" |
| 376 | << getLowerName() << "Length];\n"; |
Chandler Carruth | 38a45cc | 2015-08-04 03:53:01 +0000 | [diff] [blame] | 377 | OS << " if (!S.empty())\n"; |
| 378 | OS << " std::memcpy(this->" << getLowerName() << ", S.data(), " |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 379 | << getLowerName() << "Length);\n"; |
| 380 | OS << " }"; |
| 381 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 382 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 383 | void writeCloneArgs(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 384 | OS << "get" << getUpperName() << "()"; |
| 385 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 386 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 387 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 388 | OS << "A->get" << getUpperName() << "()"; |
| 389 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 390 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 391 | void writeCtorBody(raw_ostream &OS) const override { |
Chandler Carruth | 38a45cc | 2015-08-04 03:53:01 +0000 | [diff] [blame] | 392 | OS << " if (!" << getUpperName() << ".empty())\n"; |
| 393 | OS << " std::memcpy(" << getLowerName() << ", " << getUpperName() |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 394 | << ".data(), " << getLowerName() << "Length);\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 395 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 396 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 397 | void writeCtorInitializers(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 398 | OS << getLowerName() << "Length(" << getUpperName() << ".size())," |
| 399 | << getLowerName() << "(new (Ctx, 1) char[" << getLowerName() |
| 400 | << "Length])"; |
| 401 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 402 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 403 | void writeCtorDefaultInitializers(raw_ostream &OS) const override { |
Hans Wennborg | 59dbe86 | 2015-09-29 20:56:43 +0000 | [diff] [blame] | 404 | OS << getLowerName() << "Length(0)," << getLowerName() << "(nullptr)"; |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 405 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 406 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 407 | void writeCtorParameters(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 408 | OS << "llvm::StringRef " << getUpperName(); |
| 409 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 410 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 411 | void writeDeclarations(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 412 | OS << "unsigned " << getLowerName() << "Length;\n"; |
| 413 | OS << "char *" << getLowerName() << ";"; |
| 414 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 415 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 416 | void writePCHReadDecls(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 417 | OS << " std::string " << getLowerName() |
David L. Jones | 267b884 | 2017-01-24 01:04:30 +0000 | [diff] [blame] | 418 | << "= Record.readString();\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 419 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 420 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 421 | void writePCHReadArgs(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 422 | OS << getLowerName(); |
| 423 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 424 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 425 | void writePCHWrite(raw_ostream &OS) const override { |
Richard Smith | 290d801 | 2016-04-06 17:06:00 +0000 | [diff] [blame] | 426 | OS << " Record.AddString(SA->get" << getUpperName() << "());\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 427 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 428 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 429 | void writeValue(raw_ostream &OS) const override { |
Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 430 | OS << "\\\"\" << get" << getUpperName() << "() << \"\\\""; |
| 431 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 432 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 433 | void writeDump(raw_ostream &OS) const override { |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 434 | OS << " OS << \" \\\"\" << SA->get" << getUpperName() |
| 435 | << "() << \"\\\"\";\n"; |
| 436 | } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 437 | }; |
| 438 | |
| 439 | class AlignedArgument : public Argument { |
| 440 | public: |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 441 | AlignedArgument(const Record &Arg, StringRef Attr) |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 442 | : Argument(Arg, Attr) |
| 443 | {} |
| 444 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 445 | void writeAccessors(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 446 | OS << " bool is" << getUpperName() << "Dependent() const;\n"; |
| 447 | |
| 448 | OS << " unsigned get" << getUpperName() << "(ASTContext &Ctx) const;\n"; |
| 449 | |
| 450 | OS << " bool is" << getUpperName() << "Expr() const {\n"; |
| 451 | OS << " return is" << getLowerName() << "Expr;\n"; |
| 452 | OS << " }\n"; |
| 453 | |
| 454 | OS << " Expr *get" << getUpperName() << "Expr() const {\n"; |
| 455 | OS << " assert(is" << getLowerName() << "Expr);\n"; |
| 456 | OS << " return " << getLowerName() << "Expr;\n"; |
| 457 | OS << " }\n"; |
| 458 | |
| 459 | OS << " TypeSourceInfo *get" << getUpperName() << "Type() const {\n"; |
| 460 | OS << " assert(!is" << getLowerName() << "Expr);\n"; |
| 461 | OS << " return " << getLowerName() << "Type;\n"; |
| 462 | OS << " }"; |
| 463 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 464 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 465 | void writeAccessorDefinitions(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 466 | OS << "bool " << getAttrName() << "Attr::is" << getUpperName() |
| 467 | << "Dependent() const {\n"; |
| 468 | OS << " if (is" << getLowerName() << "Expr)\n"; |
| 469 | OS << " return " << getLowerName() << "Expr && (" << getLowerName() |
| 470 | << "Expr->isValueDependent() || " << getLowerName() |
| 471 | << "Expr->isTypeDependent());\n"; |
| 472 | OS << " else\n"; |
| 473 | OS << " return " << getLowerName() |
| 474 | << "Type->getType()->isDependentType();\n"; |
| 475 | OS << "}\n"; |
| 476 | |
| 477 | // FIXME: Do not do the calculation here |
| 478 | // FIXME: Handle types correctly |
| 479 | // A null pointer means maximum alignment |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 480 | OS << "unsigned " << getAttrName() << "Attr::get" << getUpperName() |
| 481 | << "(ASTContext &Ctx) const {\n"; |
| 482 | OS << " assert(!is" << getUpperName() << "Dependent());\n"; |
| 483 | OS << " if (is" << getLowerName() << "Expr)\n"; |
Ulrich Weigand | ca3cb7f | 2015-04-21 17:29:35 +0000 | [diff] [blame] | 484 | OS << " return " << getLowerName() << "Expr ? " << getLowerName() |
| 485 | << "Expr->EvaluateKnownConstInt(Ctx).getZExtValue()" |
| 486 | << " * Ctx.getCharWidth() : " |
| 487 | << "Ctx.getTargetDefaultAlignForAttributeAligned();\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 488 | OS << " else\n"; |
| 489 | OS << " return 0; // FIXME\n"; |
| 490 | OS << "}\n"; |
| 491 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 492 | |
Richard Smith | f26d551 | 2017-08-15 22:58:45 +0000 | [diff] [blame] | 493 | void writeASTVisitorTraversal(raw_ostream &OS) const override { |
| 494 | StringRef Name = getUpperName(); |
| 495 | OS << " if (A->is" << Name << "Expr()) {\n" |
| 496 | << " if (!getDerived().TraverseStmt(A->get" << Name << "Expr()))\n" |
| 497 | << " return false;\n" |
| 498 | << " } else if (auto *TSI = A->get" << Name << "Type()) {\n" |
| 499 | << " if (!getDerived().TraverseTypeLoc(TSI->getTypeLoc()))\n" |
| 500 | << " return false;\n" |
| 501 | << " }\n"; |
| 502 | } |
| 503 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 504 | void writeCloneArgs(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 505 | OS << "is" << getLowerName() << "Expr, is" << getLowerName() |
| 506 | << "Expr ? static_cast<void*>(" << getLowerName() |
| 507 | << "Expr) : " << getLowerName() |
| 508 | << "Type"; |
| 509 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 510 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 511 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 512 | // FIXME: move the definition in Sema::InstantiateAttrs to here. |
| 513 | // In the meantime, aligned attributes are cloned. |
| 514 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 515 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 516 | void writeCtorBody(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 517 | OS << " if (is" << getLowerName() << "Expr)\n"; |
| 518 | OS << " " << getLowerName() << "Expr = reinterpret_cast<Expr *>(" |
| 519 | << getUpperName() << ");\n"; |
| 520 | OS << " else\n"; |
| 521 | OS << " " << getLowerName() |
| 522 | << "Type = reinterpret_cast<TypeSourceInfo *>(" << getUpperName() |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 523 | << ");\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 524 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 525 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 526 | void writeCtorInitializers(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 527 | OS << "is" << getLowerName() << "Expr(Is" << getUpperName() << "Expr)"; |
| 528 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 529 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 530 | void writeCtorDefaultInitializers(raw_ostream &OS) const override { |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 531 | OS << "is" << getLowerName() << "Expr(false)"; |
| 532 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 533 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 534 | void writeCtorParameters(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 535 | OS << "bool Is" << getUpperName() << "Expr, void *" << getUpperName(); |
| 536 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 537 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 538 | void writeImplicitCtorArgs(raw_ostream &OS) const override { |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 539 | OS << "Is" << getUpperName() << "Expr, " << getUpperName(); |
| 540 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 541 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 542 | void writeDeclarations(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 543 | OS << "bool is" << getLowerName() << "Expr;\n"; |
| 544 | OS << "union {\n"; |
| 545 | OS << "Expr *" << getLowerName() << "Expr;\n"; |
| 546 | OS << "TypeSourceInfo *" << getLowerName() << "Type;\n"; |
| 547 | OS << "};"; |
| 548 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 549 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 550 | void writePCHReadArgs(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 551 | OS << "is" << getLowerName() << "Expr, " << getLowerName() << "Ptr"; |
| 552 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 553 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 554 | void writePCHReadDecls(raw_ostream &OS) const override { |
David L. Jones | 267b884 | 2017-01-24 01:04:30 +0000 | [diff] [blame] | 555 | OS << " bool is" << getLowerName() << "Expr = Record.readInt();\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 556 | OS << " void *" << getLowerName() << "Ptr;\n"; |
| 557 | OS << " if (is" << getLowerName() << "Expr)\n"; |
David L. Jones | 267b884 | 2017-01-24 01:04:30 +0000 | [diff] [blame] | 558 | OS << " " << getLowerName() << "Ptr = Record.readExpr();\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 559 | OS << " else\n"; |
| 560 | OS << " " << getLowerName() |
David L. Jones | 267b884 | 2017-01-24 01:04:30 +0000 | [diff] [blame] | 561 | << "Ptr = Record.getTypeSourceInfo();\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 562 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 563 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 564 | void writePCHWrite(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 565 | OS << " Record.push_back(SA->is" << getUpperName() << "Expr());\n"; |
| 566 | OS << " if (SA->is" << getUpperName() << "Expr())\n"; |
Richard Smith | 290d801 | 2016-04-06 17:06:00 +0000 | [diff] [blame] | 567 | OS << " Record.AddStmt(SA->get" << getUpperName() << "Expr());\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 568 | OS << " else\n"; |
Richard Smith | 290d801 | 2016-04-06 17:06:00 +0000 | [diff] [blame] | 569 | OS << " Record.AddTypeSourceInfo(SA->get" << getUpperName() |
| 570 | << "Type());\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 571 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 572 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 573 | void writeValue(raw_ostream &OS) const override { |
Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 574 | OS << "\";\n"; |
Aaron Ballman | c960f56 | 2014-08-01 13:49:00 +0000 | [diff] [blame] | 575 | // The aligned attribute argument expression is optional. |
| 576 | OS << " if (is" << getLowerName() << "Expr && " |
| 577 | << getLowerName() << "Expr)\n"; |
Hans Wennborg | 59dbe86 | 2015-09-29 20:56:43 +0000 | [diff] [blame] | 578 | OS << " " << getLowerName() << "Expr->printPretty(OS, nullptr, Policy);\n"; |
Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 579 | OS << " OS << \""; |
Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 580 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 581 | |
| 582 | void writeDump(raw_ostream &OS) const override {} |
| 583 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 584 | void writeDumpChildren(raw_ostream &OS) const override { |
Richard Smith | f751445 | 2014-10-30 21:02:37 +0000 | [diff] [blame] | 585 | OS << " if (SA->is" << getUpperName() << "Expr())\n"; |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 586 | OS << " dumpStmt(SA->get" << getUpperName() << "Expr());\n"; |
Richard Smith | f751445 | 2014-10-30 21:02:37 +0000 | [diff] [blame] | 587 | OS << " else\n"; |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 588 | OS << " dumpType(SA->get" << getUpperName() |
| 589 | << "Type()->getType());\n"; |
| 590 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 591 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 592 | void writeHasChildren(raw_ostream &OS) const override { |
Richard Trieu | de5cc7d | 2013-01-31 01:44:26 +0000 | [diff] [blame] | 593 | OS << "SA->is" << getUpperName() << "Expr()"; |
| 594 | } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 595 | }; |
| 596 | |
| 597 | class VariadicArgument : public Argument { |
Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 598 | std::string Type, ArgName, ArgSizeName, RangeName; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 599 | |
Aaron Ballman | 25a2cb9 | 2014-09-15 15:14:13 +0000 | [diff] [blame] | 600 | protected: |
| 601 | // Assumed to receive a parameter: raw_ostream OS. |
| 602 | virtual void writeValueImpl(raw_ostream &OS) const { |
| 603 | OS << " OS << Val;\n"; |
| 604 | } |
| 605 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 606 | public: |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 607 | VariadicArgument(const Record &Arg, StringRef Attr, std::string T) |
Benjamin Kramer | cfeacf5 | 2016-05-27 14:27:13 +0000 | [diff] [blame] | 608 | : Argument(Arg, Attr), Type(std::move(T)), |
| 609 | ArgName(getLowerName().str() + "_"), ArgSizeName(ArgName + "Size"), |
| 610 | RangeName(getLowerName()) {} |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 611 | |
Benjamin Kramer | 1b58201 | 2016-02-13 18:11:49 +0000 | [diff] [blame] | 612 | const std::string &getType() const { return Type; } |
| 613 | const std::string &getArgName() const { return ArgName; } |
| 614 | const std::string &getArgSizeName() const { return ArgSizeName; } |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 615 | bool isVariadic() const override { return true; } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 616 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 617 | void writeAccessors(raw_ostream &OS) const override { |
Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 618 | std::string IteratorType = getLowerName().str() + "_iterator"; |
| 619 | std::string BeginFn = getLowerName().str() + "_begin()"; |
| 620 | std::string EndFn = getLowerName().str() + "_end()"; |
| 621 | |
| 622 | OS << " typedef " << Type << "* " << IteratorType << ";\n"; |
| 623 | OS << " " << IteratorType << " " << BeginFn << " const {" |
| 624 | << " return " << ArgName << "; }\n"; |
| 625 | OS << " " << IteratorType << " " << EndFn << " const {" |
| 626 | << " return " << ArgName << " + " << ArgSizeName << "; }\n"; |
| 627 | OS << " unsigned " << getLowerName() << "_size() const {" |
| 628 | << " return " << ArgSizeName << "; }\n"; |
| 629 | OS << " llvm::iterator_range<" << IteratorType << "> " << RangeName |
| 630 | << "() const { return llvm::make_range(" << BeginFn << ", " << EndFn |
| 631 | << "); }\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 632 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 633 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 634 | void writeCloneArgs(raw_ostream &OS) const override { |
Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 635 | OS << ArgName << ", " << ArgSizeName; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 636 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 637 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 638 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 639 | // This isn't elegant, but we have to go through public methods... |
| 640 | OS << "A->" << getLowerName() << "_begin(), " |
| 641 | << "A->" << getLowerName() << "_size()"; |
| 642 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 643 | |
Richard Smith | f26d551 | 2017-08-15 22:58:45 +0000 | [diff] [blame] | 644 | void writeASTVisitorTraversal(raw_ostream &OS) const override { |
| 645 | // FIXME: Traverse the elements. |
| 646 | } |
| 647 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 648 | void writeCtorBody(raw_ostream &OS) const override { |
Aaron Ballman | d6459e5 | 2014-05-01 15:21:03 +0000 | [diff] [blame] | 649 | OS << " std::copy(" << getUpperName() << ", " << getUpperName() |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 650 | << " + " << ArgSizeName << ", " << ArgName << ");\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 651 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 652 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 653 | void writeCtorInitializers(raw_ostream &OS) const override { |
Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 654 | OS << ArgSizeName << "(" << getUpperName() << "Size), " |
| 655 | << ArgName << "(new (Ctx, 16) " << getType() << "[" |
| 656 | << ArgSizeName << "])"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 657 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 658 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 659 | void writeCtorDefaultInitializers(raw_ostream &OS) const override { |
Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 660 | OS << ArgSizeName << "(0), " << ArgName << "(nullptr)"; |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 661 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 662 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 663 | void writeCtorParameters(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 664 | OS << getType() << " *" << getUpperName() << ", unsigned " |
| 665 | << getUpperName() << "Size"; |
| 666 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 667 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 668 | void writeImplicitCtorArgs(raw_ostream &OS) const override { |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 669 | OS << getUpperName() << ", " << getUpperName() << "Size"; |
| 670 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 671 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 672 | void writeDeclarations(raw_ostream &OS) const override { |
Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 673 | OS << " unsigned " << ArgSizeName << ";\n"; |
| 674 | OS << " " << getType() << " *" << ArgName << ";"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 675 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 676 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 677 | void writePCHReadDecls(raw_ostream &OS) const override { |
David L. Jones | 267b884 | 2017-01-24 01:04:30 +0000 | [diff] [blame] | 678 | OS << " unsigned " << getLowerName() << "Size = Record.readInt();\n"; |
Richard Smith | 1997856 | 2016-05-18 00:16:51 +0000 | [diff] [blame] | 679 | OS << " SmallVector<" << getType() << ", 4> " |
| 680 | << getLowerName() << ";\n"; |
| 681 | OS << " " << getLowerName() << ".reserve(" << getLowerName() |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 682 | << "Size);\n"; |
Richard Smith | 1997856 | 2016-05-18 00:16:51 +0000 | [diff] [blame] | 683 | |
| 684 | // If we can't store the values in the current type (if it's something |
| 685 | // like StringRef), store them in a different type and convert the |
| 686 | // container afterwards. |
| 687 | std::string StorageType = getStorageType(getType()); |
| 688 | std::string StorageName = getLowerName(); |
| 689 | if (StorageType != getType()) { |
| 690 | StorageName += "Storage"; |
| 691 | OS << " SmallVector<" << StorageType << ", 4> " |
| 692 | << StorageName << ";\n"; |
| 693 | OS << " " << StorageName << ".reserve(" << getLowerName() |
| 694 | << "Size);\n"; |
| 695 | } |
| 696 | |
| 697 | OS << " for (unsigned i = 0; i != " << getLowerName() << "Size; ++i)\n"; |
Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 698 | std::string read = ReadPCHRecord(Type); |
Richard Smith | 1997856 | 2016-05-18 00:16:51 +0000 | [diff] [blame] | 699 | OS << " " << StorageName << ".push_back(" << read << ");\n"; |
| 700 | |
| 701 | if (StorageType != getType()) { |
| 702 | OS << " for (unsigned i = 0; i != " << getLowerName() << "Size; ++i)\n"; |
| 703 | OS << " " << getLowerName() << ".push_back(" |
| 704 | << StorageName << "[i]);\n"; |
| 705 | } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 706 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 707 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 708 | void writePCHReadArgs(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 709 | OS << getLowerName() << ".data(), " << getLowerName() << "Size"; |
| 710 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 711 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 712 | void writePCHWrite(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 713 | OS << " Record.push_back(SA->" << getLowerName() << "_size());\n"; |
Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 714 | OS << " for (auto &Val : SA->" << RangeName << "())\n"; |
| 715 | OS << " " << WritePCHRecord(Type, "Val"); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 716 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 717 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 718 | void writeValue(raw_ostream &OS) const override { |
Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 719 | OS << "\";\n"; |
| 720 | OS << " bool isFirst = true;\n" |
Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 721 | << " for (const auto &Val : " << RangeName << "()) {\n" |
Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 722 | << " if (isFirst) isFirst = false;\n" |
Aaron Ballman | 25a2cb9 | 2014-09-15 15:14:13 +0000 | [diff] [blame] | 723 | << " else OS << \", \";\n"; |
| 724 | writeValueImpl(OS); |
| 725 | OS << " }\n"; |
Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 726 | OS << " OS << \""; |
| 727 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 728 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 729 | void writeDump(raw_ostream &OS) const override { |
Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 730 | OS << " for (const auto &Val : SA->" << RangeName << "())\n"; |
| 731 | OS << " OS << \" \" << Val;\n"; |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 732 | } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 733 | }; |
| 734 | |
Reid Kleckner | f526b948 | 2014-02-12 18:22:18 +0000 | [diff] [blame] | 735 | // Unique the enums, but maintain the original declaration ordering. |
Craig Topper | 0064858 | 2017-05-31 19:01:22 +0000 | [diff] [blame] | 736 | std::vector<StringRef> |
| 737 | uniqueEnumsInOrder(const std::vector<StringRef> &enums) { |
| 738 | std::vector<StringRef> uniques; |
George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 739 | SmallDenseSet<StringRef, 8> unique_set; |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 740 | for (const auto &i : enums) { |
George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 741 | if (unique_set.insert(i).second) |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 742 | uniques.push_back(i); |
Reid Kleckner | f526b948 | 2014-02-12 18:22:18 +0000 | [diff] [blame] | 743 | } |
| 744 | return uniques; |
| 745 | } |
| 746 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 747 | class EnumArgument : public Argument { |
| 748 | std::string type; |
Craig Topper | 0064858 | 2017-05-31 19:01:22 +0000 | [diff] [blame] | 749 | std::vector<StringRef> values, enums, uniques; |
| 750 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 751 | public: |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 752 | EnumArgument(const Record &Arg, StringRef Attr) |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 753 | : Argument(Arg, Attr), type(Arg.getValueAsString("Type")), |
Aaron Ballman | 0e468c0 | 2014-01-05 21:08:29 +0000 | [diff] [blame] | 754 | values(Arg.getValueAsListOfStrings("Values")), |
| 755 | enums(Arg.getValueAsListOfStrings("Enums")), |
Reid Kleckner | f526b948 | 2014-02-12 18:22:18 +0000 | [diff] [blame] | 756 | uniques(uniqueEnumsInOrder(enums)) |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 757 | { |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 758 | // FIXME: Emit a proper error |
| 759 | assert(!uniques.empty()); |
| 760 | } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 761 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 762 | bool isEnumArg() const override { return true; } |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 763 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 764 | void writeAccessors(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 765 | OS << " " << type << " get" << getUpperName() << "() const {\n"; |
| 766 | OS << " return " << getLowerName() << ";\n"; |
| 767 | OS << " }"; |
| 768 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 769 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 770 | void writeCloneArgs(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 771 | OS << getLowerName(); |
| 772 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 773 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 774 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 775 | OS << "A->get" << getUpperName() << "()"; |
| 776 | } |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 777 | void writeCtorInitializers(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 778 | OS << getLowerName() << "(" << getUpperName() << ")"; |
| 779 | } |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 780 | void writeCtorDefaultInitializers(raw_ostream &OS) const override { |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 781 | OS << getLowerName() << "(" << type << "(0))"; |
| 782 | } |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 783 | void writeCtorParameters(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 784 | OS << type << " " << getUpperName(); |
| 785 | } |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 786 | void writeDeclarations(raw_ostream &OS) const override { |
Eugene Zelenko | 5f02b77 | 2015-12-08 18:49:01 +0000 | [diff] [blame] | 787 | auto i = uniques.cbegin(), e = uniques.cend(); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 788 | // The last one needs to not have a comma. |
| 789 | --e; |
| 790 | |
| 791 | OS << "public:\n"; |
| 792 | OS << " enum " << type << " {\n"; |
| 793 | for (; i != e; ++i) |
| 794 | OS << " " << *i << ",\n"; |
| 795 | OS << " " << *e << "\n"; |
| 796 | OS << " };\n"; |
| 797 | OS << "private:\n"; |
| 798 | OS << " " << type << " " << getLowerName() << ";"; |
| 799 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 800 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 801 | void writePCHReadDecls(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 802 | OS << " " << getAttrName() << "Attr::" << type << " " << getLowerName() |
| 803 | << "(static_cast<" << getAttrName() << "Attr::" << type |
David L. Jones | 267b884 | 2017-01-24 01:04:30 +0000 | [diff] [blame] | 804 | << ">(Record.readInt()));\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 805 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 806 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 807 | void writePCHReadArgs(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 808 | OS << getLowerName(); |
| 809 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 810 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 811 | void writePCHWrite(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 812 | OS << "Record.push_back(SA->get" << getUpperName() << "());\n"; |
| 813 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 814 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 815 | void writeValue(raw_ostream &OS) const override { |
Aaron Ballman | 36d7910 | 2014-09-15 16:16:14 +0000 | [diff] [blame] | 816 | // FIXME: this isn't 100% correct -- some enum arguments require printing |
| 817 | // as a string literal, while others require printing as an identifier. |
| 818 | // Tablegen currently does not distinguish between the two forms. |
Aaron Ballman | 25a2cb9 | 2014-09-15 15:14:13 +0000 | [diff] [blame] | 819 | OS << "\\\"\" << " << getAttrName() << "Attr::Convert" << type << "ToStr(get" |
| 820 | << getUpperName() << "()) << \"\\\""; |
Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 821 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 822 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 823 | void writeDump(raw_ostream &OS) const override { |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 824 | OS << " switch(SA->get" << getUpperName() << "()) {\n"; |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 825 | for (const auto &I : uniques) { |
| 826 | OS << " case " << getAttrName() << "Attr::" << I << ":\n"; |
| 827 | OS << " OS << \" " << I << "\";\n"; |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 828 | OS << " break;\n"; |
| 829 | } |
| 830 | OS << " }\n"; |
| 831 | } |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 832 | |
| 833 | void writeConversion(raw_ostream &OS) const { |
| 834 | OS << " static bool ConvertStrTo" << type << "(StringRef Val, "; |
| 835 | OS << type << " &Out) {\n"; |
| 836 | OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<"; |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 837 | OS << type << ">>(Val)\n"; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 838 | for (size_t I = 0; I < enums.size(); ++I) { |
| 839 | OS << " .Case(\"" << values[I] << "\", "; |
| 840 | OS << getAttrName() << "Attr::" << enums[I] << ")\n"; |
| 841 | } |
| 842 | OS << " .Default(Optional<" << type << ">());\n"; |
| 843 | OS << " if (R) {\n"; |
| 844 | OS << " Out = *R;\n return true;\n }\n"; |
| 845 | OS << " return false;\n"; |
Aaron Ballman | 25a2cb9 | 2014-09-15 15:14:13 +0000 | [diff] [blame] | 846 | OS << " }\n\n"; |
| 847 | |
| 848 | // Mapping from enumeration values back to enumeration strings isn't |
| 849 | // trivial because some enumeration values have multiple named |
| 850 | // enumerators, such as type_visibility(internal) and |
| 851 | // type_visibility(hidden) both mapping to TypeVisibilityAttr::Hidden. |
| 852 | OS << " static const char *Convert" << type << "ToStr(" |
| 853 | << type << " Val) {\n" |
| 854 | << " switch(Val) {\n"; |
George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 855 | SmallDenseSet<StringRef, 8> Uniques; |
Aaron Ballman | 25a2cb9 | 2014-09-15 15:14:13 +0000 | [diff] [blame] | 856 | for (size_t I = 0; I < enums.size(); ++I) { |
| 857 | if (Uniques.insert(enums[I]).second) |
| 858 | OS << " case " << getAttrName() << "Attr::" << enums[I] |
| 859 | << ": return \"" << values[I] << "\";\n"; |
| 860 | } |
| 861 | OS << " }\n" |
| 862 | << " llvm_unreachable(\"No enumerator with that value\");\n" |
| 863 | << " }\n"; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 864 | } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 865 | }; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 866 | |
| 867 | class VariadicEnumArgument: public VariadicArgument { |
| 868 | std::string type, QualifiedTypeName; |
Craig Topper | 0064858 | 2017-05-31 19:01:22 +0000 | [diff] [blame] | 869 | std::vector<StringRef> values, enums, uniques; |
Aaron Ballman | 25a2cb9 | 2014-09-15 15:14:13 +0000 | [diff] [blame] | 870 | |
| 871 | protected: |
| 872 | void writeValueImpl(raw_ostream &OS) const override { |
Aaron Ballman | 36d7910 | 2014-09-15 16:16:14 +0000 | [diff] [blame] | 873 | // FIXME: this isn't 100% correct -- some enum arguments require printing |
| 874 | // as a string literal, while others require printing as an identifier. |
| 875 | // Tablegen currently does not distinguish between the two forms. |
Aaron Ballman | 25a2cb9 | 2014-09-15 15:14:13 +0000 | [diff] [blame] | 876 | OS << " OS << \"\\\"\" << " << getAttrName() << "Attr::Convert" << type |
| 877 | << "ToStr(Val)" << "<< \"\\\"\";\n"; |
| 878 | } |
| 879 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 880 | public: |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 881 | VariadicEnumArgument(const Record &Arg, StringRef Attr) |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 882 | : VariadicArgument(Arg, Attr, Arg.getValueAsString("Type")), |
| 883 | type(Arg.getValueAsString("Type")), |
Aaron Ballman | 0e468c0 | 2014-01-05 21:08:29 +0000 | [diff] [blame] | 884 | values(Arg.getValueAsListOfStrings("Values")), |
| 885 | enums(Arg.getValueAsListOfStrings("Enums")), |
Reid Kleckner | f526b948 | 2014-02-12 18:22:18 +0000 | [diff] [blame] | 886 | uniques(uniqueEnumsInOrder(enums)) |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 887 | { |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 888 | QualifiedTypeName = getAttrName().str() + "Attr::" + type; |
| 889 | |
| 890 | // FIXME: Emit a proper error |
| 891 | assert(!uniques.empty()); |
| 892 | } |
| 893 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 894 | bool isVariadicEnumArg() const override { return true; } |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 895 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 896 | void writeDeclarations(raw_ostream &OS) const override { |
Eugene Zelenko | 5f02b77 | 2015-12-08 18:49:01 +0000 | [diff] [blame] | 897 | auto i = uniques.cbegin(), e = uniques.cend(); |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 898 | // The last one needs to not have a comma. |
| 899 | --e; |
| 900 | |
| 901 | OS << "public:\n"; |
| 902 | OS << " enum " << type << " {\n"; |
| 903 | for (; i != e; ++i) |
| 904 | OS << " " << *i << ",\n"; |
| 905 | OS << " " << *e << "\n"; |
| 906 | OS << " };\n"; |
| 907 | OS << "private:\n"; |
| 908 | |
| 909 | VariadicArgument::writeDeclarations(OS); |
| 910 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 911 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 912 | void writeDump(raw_ostream &OS) const override { |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 913 | OS << " for (" << getAttrName() << "Attr::" << getLowerName() |
| 914 | << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->" |
| 915 | << getLowerName() << "_end(); I != E; ++I) {\n"; |
| 916 | OS << " switch(*I) {\n"; |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 917 | for (const auto &UI : uniques) { |
| 918 | OS << " case " << getAttrName() << "Attr::" << UI << ":\n"; |
| 919 | OS << " OS << \" " << UI << "\";\n"; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 920 | OS << " break;\n"; |
| 921 | } |
| 922 | OS << " }\n"; |
| 923 | OS << " }\n"; |
| 924 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 925 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 926 | void writePCHReadDecls(raw_ostream &OS) const override { |
David L. Jones | 267b884 | 2017-01-24 01:04:30 +0000 | [diff] [blame] | 927 | OS << " unsigned " << getLowerName() << "Size = Record.readInt();\n"; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 928 | OS << " SmallVector<" << QualifiedTypeName << ", 4> " << getLowerName() |
| 929 | << ";\n"; |
| 930 | OS << " " << getLowerName() << ".reserve(" << getLowerName() |
| 931 | << "Size);\n"; |
| 932 | OS << " for (unsigned i = " << getLowerName() << "Size; i; --i)\n"; |
| 933 | OS << " " << getLowerName() << ".push_back(" << "static_cast<" |
David L. Jones | 267b884 | 2017-01-24 01:04:30 +0000 | [diff] [blame] | 934 | << QualifiedTypeName << ">(Record.readInt()));\n"; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 935 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 936 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 937 | void writePCHWrite(raw_ostream &OS) const override { |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 938 | OS << " Record.push_back(SA->" << getLowerName() << "_size());\n"; |
| 939 | OS << " for (" << getAttrName() << "Attr::" << getLowerName() |
| 940 | << "_iterator i = SA->" << getLowerName() << "_begin(), e = SA->" |
| 941 | << getLowerName() << "_end(); i != e; ++i)\n"; |
| 942 | OS << " " << WritePCHRecord(QualifiedTypeName, "(*i)"); |
| 943 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 944 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 945 | void writeConversion(raw_ostream &OS) const { |
| 946 | OS << " static bool ConvertStrTo" << type << "(StringRef Val, "; |
| 947 | OS << type << " &Out) {\n"; |
| 948 | OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<"; |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 949 | OS << type << ">>(Val)\n"; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 950 | for (size_t I = 0; I < enums.size(); ++I) { |
| 951 | OS << " .Case(\"" << values[I] << "\", "; |
| 952 | OS << getAttrName() << "Attr::" << enums[I] << ")\n"; |
| 953 | } |
| 954 | OS << " .Default(Optional<" << type << ">());\n"; |
| 955 | OS << " if (R) {\n"; |
| 956 | OS << " Out = *R;\n return true;\n }\n"; |
| 957 | OS << " return false;\n"; |
Aaron Ballman | 25a2cb9 | 2014-09-15 15:14:13 +0000 | [diff] [blame] | 958 | OS << " }\n\n"; |
| 959 | |
| 960 | OS << " static const char *Convert" << type << "ToStr(" |
| 961 | << type << " Val) {\n" |
| 962 | << " switch(Val) {\n"; |
George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 963 | SmallDenseSet<StringRef, 8> Uniques; |
Aaron Ballman | 25a2cb9 | 2014-09-15 15:14:13 +0000 | [diff] [blame] | 964 | for (size_t I = 0; I < enums.size(); ++I) { |
| 965 | if (Uniques.insert(enums[I]).second) |
| 966 | OS << " case " << getAttrName() << "Attr::" << enums[I] |
| 967 | << ": return \"" << values[I] << "\";\n"; |
| 968 | } |
| 969 | OS << " }\n" |
| 970 | << " llvm_unreachable(\"No enumerator with that value\");\n" |
| 971 | << " }\n"; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 972 | } |
| 973 | }; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 974 | |
| 975 | class VersionArgument : public Argument { |
| 976 | public: |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 977 | VersionArgument(const Record &Arg, StringRef Attr) |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 978 | : Argument(Arg, Attr) |
| 979 | {} |
| 980 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 981 | void writeAccessors(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 982 | OS << " VersionTuple get" << getUpperName() << "() const {\n"; |
| 983 | OS << " return " << getLowerName() << ";\n"; |
| 984 | OS << " }\n"; |
| 985 | OS << " void set" << getUpperName() |
| 986 | << "(ASTContext &C, VersionTuple V) {\n"; |
| 987 | OS << " " << getLowerName() << " = V;\n"; |
| 988 | OS << " }"; |
| 989 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 990 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 991 | void writeCloneArgs(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 992 | OS << "get" << getUpperName() << "()"; |
| 993 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 994 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 995 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 996 | OS << "A->get" << getUpperName() << "()"; |
| 997 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 998 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 999 | void writeCtorInitializers(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1000 | OS << getLowerName() << "(" << getUpperName() << ")"; |
| 1001 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1002 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1003 | void writeCtorDefaultInitializers(raw_ostream &OS) const override { |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 1004 | OS << getLowerName() << "()"; |
| 1005 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1006 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1007 | void writeCtorParameters(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1008 | OS << "VersionTuple " << getUpperName(); |
| 1009 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1010 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1011 | void writeDeclarations(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1012 | OS << "VersionTuple " << getLowerName() << ";\n"; |
| 1013 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1014 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1015 | void writePCHReadDecls(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1016 | OS << " VersionTuple " << getLowerName() |
David L. Jones | 267b884 | 2017-01-24 01:04:30 +0000 | [diff] [blame] | 1017 | << "= Record.readVersionTuple();\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1018 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1019 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1020 | void writePCHReadArgs(raw_ostream &OS) const override { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1021 | OS << getLowerName(); |
| 1022 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1023 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1024 | void writePCHWrite(raw_ostream &OS) const override { |
Richard Smith | 290d801 | 2016-04-06 17:06:00 +0000 | [diff] [blame] | 1025 | OS << " Record.AddVersionTuple(SA->get" << getUpperName() << "());\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1026 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1027 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1028 | void writeValue(raw_ostream &OS) const override { |
Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 1029 | OS << getLowerName() << "=\" << get" << getUpperName() << "() << \""; |
| 1030 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1031 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1032 | void writeDump(raw_ostream &OS) const override { |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 1033 | OS << " OS << \" \" << SA->get" << getUpperName() << "();\n"; |
| 1034 | } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1035 | }; |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1036 | |
| 1037 | class ExprArgument : public SimpleArgument { |
| 1038 | public: |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 1039 | ExprArgument(const Record &Arg, StringRef Attr) |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1040 | : SimpleArgument(Arg, Attr, "Expr *") |
| 1041 | {} |
| 1042 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1043 | void writeASTVisitorTraversal(raw_ostream &OS) const override { |
DeLesley Hutchins | c4a8243 | 2013-12-30 17:24:36 +0000 | [diff] [blame] | 1044 | OS << " if (!" |
| 1045 | << "getDerived().TraverseStmt(A->get" << getUpperName() << "()))\n"; |
| 1046 | OS << " return false;\n"; |
| 1047 | } |
| 1048 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1049 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1050 | OS << "tempInst" << getUpperName(); |
| 1051 | } |
| 1052 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1053 | void writeTemplateInstantiation(raw_ostream &OS) const override { |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1054 | OS << " " << getType() << " tempInst" << getUpperName() << ";\n"; |
| 1055 | OS << " {\n"; |
| 1056 | OS << " EnterExpressionEvaluationContext " |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 1057 | << "Unevaluated(S, Sema::ExpressionEvaluationContext::Unevaluated);\n"; |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1058 | OS << " ExprResult " << "Result = S.SubstExpr(" |
| 1059 | << "A->get" << getUpperName() << "(), TemplateArgs);\n"; |
| 1060 | OS << " tempInst" << getUpperName() << " = " |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1061 | << "Result.getAs<Expr>();\n"; |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1062 | OS << " }\n"; |
| 1063 | } |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 1064 | |
Craig Topper | 3164f33 | 2014-03-11 03:39:26 +0000 | [diff] [blame] | 1065 | void writeDump(raw_ostream &OS) const override {} |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 1066 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1067 | void writeDumpChildren(raw_ostream &OS) const override { |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 1068 | OS << " dumpStmt(SA->get" << getUpperName() << "());\n"; |
| 1069 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1070 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1071 | void writeHasChildren(raw_ostream &OS) const override { OS << "true"; } |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1072 | }; |
| 1073 | |
| 1074 | class VariadicExprArgument : public VariadicArgument { |
| 1075 | public: |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 1076 | VariadicExprArgument(const Record &Arg, StringRef Attr) |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1077 | : VariadicArgument(Arg, Attr, "Expr *") |
| 1078 | {} |
| 1079 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1080 | void writeASTVisitorTraversal(raw_ostream &OS) const override { |
DeLesley Hutchins | c4a8243 | 2013-12-30 17:24:36 +0000 | [diff] [blame] | 1081 | OS << " {\n"; |
| 1082 | OS << " " << getType() << " *I = A->" << getLowerName() |
| 1083 | << "_begin();\n"; |
| 1084 | OS << " " << getType() << " *E = A->" << getLowerName() |
| 1085 | << "_end();\n"; |
| 1086 | OS << " for (; I != E; ++I) {\n"; |
| 1087 | OS << " if (!getDerived().TraverseStmt(*I))\n"; |
| 1088 | OS << " return false;\n"; |
| 1089 | OS << " }\n"; |
| 1090 | OS << " }\n"; |
| 1091 | } |
| 1092 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1093 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1094 | OS << "tempInst" << getUpperName() << ", " |
| 1095 | << "A->" << getLowerName() << "_size()"; |
| 1096 | } |
| 1097 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1098 | void writeTemplateInstantiation(raw_ostream &OS) const override { |
Eugene Zelenko | 5f02b77 | 2015-12-08 18:49:01 +0000 | [diff] [blame] | 1099 | OS << " auto *tempInst" << getUpperName() |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1100 | << " = new (C, 16) " << getType() |
| 1101 | << "[A->" << getLowerName() << "_size()];\n"; |
| 1102 | OS << " {\n"; |
| 1103 | OS << " EnterExpressionEvaluationContext " |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 1104 | << "Unevaluated(S, Sema::ExpressionEvaluationContext::Unevaluated);\n"; |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1105 | OS << " " << getType() << " *TI = tempInst" << getUpperName() |
| 1106 | << ";\n"; |
| 1107 | OS << " " << getType() << " *I = A->" << getLowerName() |
| 1108 | << "_begin();\n"; |
| 1109 | OS << " " << getType() << " *E = A->" << getLowerName() |
| 1110 | << "_end();\n"; |
| 1111 | OS << " for (; I != E; ++I, ++TI) {\n"; |
| 1112 | OS << " ExprResult Result = S.SubstExpr(*I, TemplateArgs);\n"; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1113 | OS << " *TI = Result.getAs<Expr>();\n"; |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1114 | OS << " }\n"; |
| 1115 | OS << " }\n"; |
| 1116 | } |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 1117 | |
Craig Topper | 3164f33 | 2014-03-11 03:39:26 +0000 | [diff] [blame] | 1118 | void writeDump(raw_ostream &OS) const override {} |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 1119 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1120 | void writeDumpChildren(raw_ostream &OS) const override { |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 1121 | OS << " for (" << getAttrName() << "Attr::" << getLowerName() |
| 1122 | << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->" |
Richard Smith | f751445 | 2014-10-30 21:02:37 +0000 | [diff] [blame] | 1123 | << getLowerName() << "_end(); I != E; ++I)\n"; |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 1124 | OS << " dumpStmt(*I);\n"; |
Richard Trieu | de5cc7d | 2013-01-31 01:44:26 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1127 | void writeHasChildren(raw_ostream &OS) const override { |
Richard Trieu | de5cc7d | 2013-01-31 01:44:26 +0000 | [diff] [blame] | 1128 | OS << "SA->" << getLowerName() << "_begin() != " |
| 1129 | << "SA->" << getLowerName() << "_end()"; |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 1130 | } |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1131 | }; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1132 | |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 1133 | class VariadicStringArgument : public VariadicArgument { |
| 1134 | public: |
| 1135 | VariadicStringArgument(const Record &Arg, StringRef Attr) |
Benjamin Kramer | 1b58201 | 2016-02-13 18:11:49 +0000 | [diff] [blame] | 1136 | : VariadicArgument(Arg, Attr, "StringRef") |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 1137 | {} |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1138 | |
Benjamin Kramer | 1b58201 | 2016-02-13 18:11:49 +0000 | [diff] [blame] | 1139 | void writeCtorBody(raw_ostream &OS) const override { |
| 1140 | OS << " for (size_t I = 0, E = " << getArgSizeName() << "; I != E;\n" |
| 1141 | " ++I) {\n" |
| 1142 | " StringRef Ref = " << getUpperName() << "[I];\n" |
| 1143 | " if (!Ref.empty()) {\n" |
| 1144 | " char *Mem = new (Ctx, 1) char[Ref.size()];\n" |
| 1145 | " std::memcpy(Mem, Ref.data(), Ref.size());\n" |
| 1146 | " " << getArgName() << "[I] = StringRef(Mem, Ref.size());\n" |
| 1147 | " }\n" |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1148 | " }\n"; |
Benjamin Kramer | 1b58201 | 2016-02-13 18:11:49 +0000 | [diff] [blame] | 1149 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1150 | |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 1151 | void writeValueImpl(raw_ostream &OS) const override { |
| 1152 | OS << " OS << \"\\\"\" << Val << \"\\\"\";\n"; |
| 1153 | } |
| 1154 | }; |
| 1155 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1156 | class TypeArgument : public SimpleArgument { |
| 1157 | public: |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 1158 | TypeArgument(const Record &Arg, StringRef Attr) |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1159 | : SimpleArgument(Arg, Attr, "TypeSourceInfo *") |
| 1160 | {} |
| 1161 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1162 | void writeAccessors(raw_ostream &OS) const override { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1163 | OS << " QualType get" << getUpperName() << "() const {\n"; |
| 1164 | OS << " return " << getLowerName() << "->getType();\n"; |
| 1165 | OS << " }"; |
| 1166 | OS << " " << getType() << " get" << getUpperName() << "Loc() const {\n"; |
| 1167 | OS << " return " << getLowerName() << ";\n"; |
| 1168 | OS << " }"; |
| 1169 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1170 | |
Richard Smith | f26d551 | 2017-08-15 22:58:45 +0000 | [diff] [blame] | 1171 | void writeASTVisitorTraversal(raw_ostream &OS) const override { |
| 1172 | OS << " if (auto *TSI = A->get" << getUpperName() << "Loc())\n"; |
| 1173 | OS << " if (!getDerived().TraverseTypeLoc(TSI->getTypeLoc()))\n"; |
| 1174 | OS << " return false;\n"; |
| 1175 | } |
| 1176 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1177 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1178 | OS << "A->get" << getUpperName() << "Loc()"; |
| 1179 | } |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1180 | |
Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1181 | void writePCHWrite(raw_ostream &OS) const override { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1182 | OS << " " << WritePCHRecord( |
| 1183 | getType(), "SA->get" + std::string(getUpperName()) + "Loc()"); |
| 1184 | } |
| 1185 | }; |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1186 | |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1187 | } // end anonymous namespace |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1188 | |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 1189 | static std::unique_ptr<Argument> |
| 1190 | createArgument(const Record &Arg, StringRef Attr, |
| 1191 | const Record *Search = nullptr) { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1192 | if (!Search) |
| 1193 | Search = &Arg; |
| 1194 | |
David Blaikie | 28f30ca | 2014-08-08 23:59:38 +0000 | [diff] [blame] | 1195 | std::unique_ptr<Argument> Ptr; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1196 | llvm::StringRef ArgName = Search->getName(); |
| 1197 | |
David Blaikie | 28f30ca | 2014-08-08 23:59:38 +0000 | [diff] [blame] | 1198 | if (ArgName == "AlignedArgument") |
| 1199 | Ptr = llvm::make_unique<AlignedArgument>(Arg, Attr); |
| 1200 | else if (ArgName == "EnumArgument") |
| 1201 | Ptr = llvm::make_unique<EnumArgument>(Arg, Attr); |
| 1202 | else if (ArgName == "ExprArgument") |
| 1203 | Ptr = llvm::make_unique<ExprArgument>(Arg, Attr); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1204 | else if (ArgName == "FunctionArgument") |
David Blaikie | 28f30ca | 2014-08-08 23:59:38 +0000 | [diff] [blame] | 1205 | Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "FunctionDecl *"); |
Argyrios Kyrtzidis | a7233bd | 2017-05-24 00:46:27 +0000 | [diff] [blame] | 1206 | else if (ArgName == "NamedArgument") |
| 1207 | Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "NamedDecl *"); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1208 | else if (ArgName == "IdentifierArgument") |
David Blaikie | 28f30ca | 2014-08-08 23:59:38 +0000 | [diff] [blame] | 1209 | Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "IdentifierInfo *"); |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 1210 | else if (ArgName == "DefaultBoolArgument") |
David Blaikie | 28f30ca | 2014-08-08 23:59:38 +0000 | [diff] [blame] | 1211 | Ptr = llvm::make_unique<DefaultSimpleArgument>( |
| 1212 | Arg, Attr, "bool", Arg.getValueAsBit("Default")); |
| 1213 | else if (ArgName == "BoolArgument") |
| 1214 | Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "bool"); |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 1215 | else if (ArgName == "DefaultIntArgument") |
David Blaikie | 28f30ca | 2014-08-08 23:59:38 +0000 | [diff] [blame] | 1216 | Ptr = llvm::make_unique<DefaultSimpleArgument>( |
| 1217 | Arg, Attr, "int", Arg.getValueAsInt("Default")); |
| 1218 | else if (ArgName == "IntArgument") |
| 1219 | Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "int"); |
| 1220 | else if (ArgName == "StringArgument") |
| 1221 | Ptr = llvm::make_unique<StringArgument>(Arg, Attr); |
| 1222 | else if (ArgName == "TypeArgument") |
| 1223 | Ptr = llvm::make_unique<TypeArgument>(Arg, Attr); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1224 | else if (ArgName == "UnsignedArgument") |
David Blaikie | 28f30ca | 2014-08-08 23:59:38 +0000 | [diff] [blame] | 1225 | Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "unsigned"); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1226 | else if (ArgName == "VariadicUnsignedArgument") |
David Blaikie | 28f30ca | 2014-08-08 23:59:38 +0000 | [diff] [blame] | 1227 | Ptr = llvm::make_unique<VariadicArgument>(Arg, Attr, "unsigned"); |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 1228 | else if (ArgName == "VariadicStringArgument") |
| 1229 | Ptr = llvm::make_unique<VariadicStringArgument>(Arg, Attr); |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1230 | else if (ArgName == "VariadicEnumArgument") |
David Blaikie | 28f30ca | 2014-08-08 23:59:38 +0000 | [diff] [blame] | 1231 | Ptr = llvm::make_unique<VariadicEnumArgument>(Arg, Attr); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1232 | else if (ArgName == "VariadicExprArgument") |
David Blaikie | 28f30ca | 2014-08-08 23:59:38 +0000 | [diff] [blame] | 1233 | Ptr = llvm::make_unique<VariadicExprArgument>(Arg, Attr); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1234 | else if (ArgName == "VersionArgument") |
David Blaikie | 28f30ca | 2014-08-08 23:59:38 +0000 | [diff] [blame] | 1235 | Ptr = llvm::make_unique<VersionArgument>(Arg, Attr); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1236 | |
| 1237 | if (!Ptr) { |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 1238 | // Search in reverse order so that the most-derived type is handled first. |
Craig Topper | 2576124 | 2016-01-18 19:52:54 +0000 | [diff] [blame] | 1239 | ArrayRef<std::pair<Record*, SMRange>> Bases = Search->getSuperClasses(); |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 1240 | for (const auto &Base : llvm::reverse(Bases)) { |
Craig Topper | 2576124 | 2016-01-18 19:52:54 +0000 | [diff] [blame] | 1241 | if ((Ptr = createArgument(Arg, Attr, Base.first))) |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1242 | break; |
| 1243 | } |
| 1244 | } |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 1245 | |
| 1246 | if (Ptr && Arg.getValueAsBit("Optional")) |
| 1247 | Ptr->setOptional(true); |
| 1248 | |
John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 1249 | if (Ptr && Arg.getValueAsBit("Fake")) |
| 1250 | Ptr->setFake(true); |
| 1251 | |
David Blaikie | 28f30ca | 2014-08-08 23:59:38 +0000 | [diff] [blame] | 1252 | return Ptr; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 1255 | static void writeAvailabilityValue(raw_ostream &OS) { |
| 1256 | OS << "\" << getPlatform()->getName();\n" |
Manman Ren | 42e09eb | 2016-03-10 23:54:12 +0000 | [diff] [blame] | 1257 | << " if (getStrict()) OS << \", strict\";\n" |
Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 1258 | << " if (!getIntroduced().empty()) OS << \", introduced=\" << getIntroduced();\n" |
| 1259 | << " if (!getDeprecated().empty()) OS << \", deprecated=\" << getDeprecated();\n" |
| 1260 | << " if (!getObsoleted().empty()) OS << \", obsoleted=\" << getObsoleted();\n" |
| 1261 | << " if (getUnavailable()) OS << \", unavailable\";\n" |
| 1262 | << " OS << \""; |
| 1263 | } |
| 1264 | |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 1265 | static void writeDeprecatedAttrValue(raw_ostream &OS, std::string &Variety) { |
| 1266 | OS << "\\\"\" << getMessage() << \"\\\"\";\n"; |
| 1267 | // Only GNU deprecated has an optional fixit argument at the second position. |
| 1268 | if (Variety == "GNU") |
| 1269 | OS << " if (!getReplacement().empty()) OS << \", \\\"\"" |
| 1270 | " << getReplacement() << \"\\\"\";\n"; |
| 1271 | OS << " OS << \""; |
| 1272 | } |
| 1273 | |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 1274 | static void writeGetSpellingFunction(Record &R, raw_ostream &OS) { |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1275 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R); |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 1276 | |
| 1277 | OS << "const char *" << R.getName() << "Attr::getSpelling() const {\n"; |
| 1278 | if (Spellings.empty()) { |
| 1279 | OS << " return \"(No spelling)\";\n}\n\n"; |
| 1280 | return; |
| 1281 | } |
| 1282 | |
| 1283 | OS << " switch (SpellingListIndex) {\n" |
| 1284 | " default:\n" |
| 1285 | " llvm_unreachable(\"Unknown attribute spelling!\");\n" |
| 1286 | " return \"(No spelling)\";\n"; |
| 1287 | |
| 1288 | for (unsigned I = 0; I < Spellings.size(); ++I) |
| 1289 | OS << " case " << I << ":\n" |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1290 | " return \"" << Spellings[I].name() << "\";\n"; |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 1291 | // End of the switch statement. |
| 1292 | OS << " }\n"; |
| 1293 | // End of the getSpelling function. |
| 1294 | OS << "}\n\n"; |
| 1295 | } |
| 1296 | |
Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 1297 | static void |
| 1298 | writePrettyPrintFunction(Record &R, |
| 1299 | const std::vector<std::unique_ptr<Argument>> &Args, |
| 1300 | raw_ostream &OS) { |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1301 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1302 | |
| 1303 | OS << "void " << R.getName() << "Attr::printPretty(" |
| 1304 | << "raw_ostream &OS, const PrintingPolicy &Policy) const {\n"; |
| 1305 | |
Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 1306 | if (Spellings.empty()) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1307 | OS << "}\n\n"; |
| 1308 | return; |
| 1309 | } |
| 1310 | |
| 1311 | OS << |
| 1312 | " switch (SpellingListIndex) {\n" |
| 1313 | " default:\n" |
| 1314 | " llvm_unreachable(\"Unknown attribute spelling!\");\n" |
| 1315 | " break;\n"; |
| 1316 | |
| 1317 | for (unsigned I = 0; I < Spellings.size(); ++ I) { |
| 1318 | llvm::SmallString<16> Prefix; |
| 1319 | llvm::SmallString<8> Suffix; |
| 1320 | // The actual spelling of the name and namespace (if applicable) |
| 1321 | // of an attribute without considering prefix and suffix. |
| 1322 | llvm::SmallString<64> Spelling; |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1323 | std::string Name = Spellings[I].name(); |
| 1324 | std::string Variety = Spellings[I].variety(); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1325 | |
| 1326 | if (Variety == "GNU") { |
| 1327 | Prefix = " __attribute__(("; |
| 1328 | Suffix = "))"; |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame^] | 1329 | } else if (Variety == "CXX11" || Variety == "C2x") { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1330 | Prefix = " [["; |
| 1331 | Suffix = "]]"; |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1332 | std::string Namespace = Spellings[I].nameSpace(); |
Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 1333 | if (!Namespace.empty()) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1334 | Spelling += Namespace; |
| 1335 | Spelling += "::"; |
| 1336 | } |
| 1337 | } else if (Variety == "Declspec") { |
| 1338 | Prefix = " __declspec("; |
| 1339 | Suffix = ")"; |
Nico Weber | 20e0804 | 2016-09-03 02:55:10 +0000 | [diff] [blame] | 1340 | } else if (Variety == "Microsoft") { |
| 1341 | Prefix = "["; |
| 1342 | Suffix = "]"; |
Richard Smith | 0cdcc98 | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 1343 | } else if (Variety == "Keyword") { |
| 1344 | Prefix = " "; |
| 1345 | Suffix = ""; |
Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 1346 | } else if (Variety == "Pragma") { |
| 1347 | Prefix = "#pragma "; |
| 1348 | Suffix = "\n"; |
| 1349 | std::string Namespace = Spellings[I].nameSpace(); |
| 1350 | if (!Namespace.empty()) { |
| 1351 | Spelling += Namespace; |
| 1352 | Spelling += " "; |
| 1353 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1354 | } else { |
Richard Smith | 0cdcc98 | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 1355 | llvm_unreachable("Unknown attribute syntax variety!"); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | Spelling += Name; |
| 1359 | |
| 1360 | OS << |
| 1361 | " case " << I << " : {\n" |
Yaron Keren | 09fb7c6 | 2015-03-10 07:33:23 +0000 | [diff] [blame] | 1362 | " OS << \"" << Prefix << Spelling; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1363 | |
Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 1364 | if (Variety == "Pragma") { |
| 1365 | OS << " \";\n"; |
| 1366 | OS << " printPrettyPragma(OS, Policy);\n"; |
Alexey Bataev | 6d45532 | 2015-10-12 06:59:48 +0000 | [diff] [blame] | 1367 | OS << " OS << \"\\n\";"; |
Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 1368 | OS << " break;\n"; |
| 1369 | OS << " }\n"; |
| 1370 | continue; |
| 1371 | } |
| 1372 | |
John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 1373 | // Fake arguments aren't part of the parsed form and should not be |
| 1374 | // pretty-printed. |
George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 1375 | bool hasNonFakeArgs = llvm::any_of( |
| 1376 | Args, [](const std::unique_ptr<Argument> &A) { return !A->isFake(); }); |
John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 1377 | |
Aaron Ballman | c960f56 | 2014-08-01 13:49:00 +0000 | [diff] [blame] | 1378 | // FIXME: always printing the parenthesis isn't the correct behavior for |
| 1379 | // attributes which have optional arguments that were not provided. For |
| 1380 | // instance: __attribute__((aligned)) will be pretty printed as |
| 1381 | // __attribute__((aligned())). The logic should check whether there is only |
| 1382 | // a single argument, and if it is optional, whether it has been provided. |
John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 1383 | if (hasNonFakeArgs) |
Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 1384 | OS << "("; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1385 | if (Spelling == "availability") { |
| 1386 | writeAvailabilityValue(OS); |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 1387 | } else if (Spelling == "deprecated" || Spelling == "gnu::deprecated") { |
| 1388 | writeDeprecatedAttrValue(OS, Variety); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1389 | } else { |
John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 1390 | unsigned index = 0; |
| 1391 | for (const auto &arg : Args) { |
| 1392 | if (arg->isFake()) continue; |
| 1393 | if (index++) OS << ", "; |
| 1394 | arg->writeValue(OS); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1395 | } |
| 1396 | } |
| 1397 | |
John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 1398 | if (hasNonFakeArgs) |
Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 1399 | OS << ")"; |
Yaron Keren | 09fb7c6 | 2015-03-10 07:33:23 +0000 | [diff] [blame] | 1400 | OS << Suffix + "\";\n"; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1401 | |
| 1402 | OS << |
| 1403 | " break;\n" |
| 1404 | " }\n"; |
| 1405 | } |
| 1406 | |
| 1407 | // End of the switch statement. |
| 1408 | OS << "}\n"; |
| 1409 | // End of the print function. |
| 1410 | OS << "}\n\n"; |
| 1411 | } |
| 1412 | |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 1413 | /// \brief Return the index of a spelling in a spelling list. |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1414 | static unsigned |
| 1415 | getSpellingListIndex(const std::vector<FlattenedSpelling> &SpellingList, |
| 1416 | const FlattenedSpelling &Spelling) { |
Alexander Kornienko | 6ee521c | 2015-01-23 15:36:10 +0000 | [diff] [blame] | 1417 | assert(!SpellingList.empty() && "Spelling list is empty!"); |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 1418 | |
| 1419 | for (unsigned Index = 0; Index < SpellingList.size(); ++Index) { |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1420 | const FlattenedSpelling &S = SpellingList[Index]; |
| 1421 | if (S.variety() != Spelling.variety()) |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 1422 | continue; |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1423 | if (S.nameSpace() != Spelling.nameSpace()) |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 1424 | continue; |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1425 | if (S.name() != Spelling.name()) |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 1426 | continue; |
| 1427 | |
| 1428 | return Index; |
| 1429 | } |
| 1430 | |
| 1431 | llvm_unreachable("Unknown spelling!"); |
| 1432 | } |
| 1433 | |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 1434 | static void writeAttrAccessorDefinition(const Record &R, raw_ostream &OS) { |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 1435 | std::vector<Record*> Accessors = R.getValueAsListOfDefs("Accessors"); |
George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 1436 | if (Accessors.empty()) |
| 1437 | return; |
| 1438 | |
| 1439 | const std::vector<FlattenedSpelling> SpellingList = GetFlattenedSpellings(R); |
| 1440 | assert(!SpellingList.empty() && |
| 1441 | "Attribute with empty spelling list can't have accessors!"); |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 1442 | for (const auto *Accessor : Accessors) { |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 1443 | std::string Name = Accessor->getValueAsString("Name"); |
George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 1444 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Accessor); |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 1445 | |
| 1446 | OS << " bool " << Name << "() const { return SpellingListIndex == "; |
| 1447 | for (unsigned Index = 0; Index < Spellings.size(); ++Index) { |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1448 | OS << getSpellingListIndex(SpellingList, Spellings[Index]); |
George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 1449 | if (Index != Spellings.size() - 1) |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 1450 | OS << " ||\n SpellingListIndex == "; |
| 1451 | else |
| 1452 | OS << "; }\n"; |
| 1453 | } |
| 1454 | } |
| 1455 | } |
| 1456 | |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1457 | static bool |
| 1458 | SpellingNamesAreCommon(const std::vector<FlattenedSpelling>& Spellings) { |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 1459 | assert(!Spellings.empty() && "An empty list of spellings was provided"); |
| 1460 | std::string FirstName = NormalizeNameForSpellingComparison( |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1461 | Spellings.front().name()); |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 1462 | for (const auto &Spelling : |
| 1463 | llvm::make_range(std::next(Spellings.begin()), Spellings.end())) { |
| 1464 | std::string Name = NormalizeNameForSpellingComparison(Spelling.name()); |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 1465 | if (Name != FirstName) |
| 1466 | return false; |
| 1467 | } |
| 1468 | return true; |
| 1469 | } |
| 1470 | |
Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 1471 | typedef std::map<unsigned, std::string> SemanticSpellingMap; |
| 1472 | static std::string |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1473 | CreateSemanticSpellings(const std::vector<FlattenedSpelling> &Spellings, |
Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 1474 | SemanticSpellingMap &Map) { |
| 1475 | // The enumerants are automatically generated based on the variety, |
| 1476 | // namespace (if present) and name for each attribute spelling. However, |
| 1477 | // care is taken to avoid trampling on the reserved namespace due to |
| 1478 | // underscores. |
| 1479 | std::string Ret(" enum Spelling {\n"); |
| 1480 | std::set<std::string> Uniques; |
| 1481 | unsigned Idx = 0; |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 1482 | for (auto I = Spellings.begin(), E = Spellings.end(); I != E; ++I, ++Idx) { |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1483 | const FlattenedSpelling &S = *I; |
Benjamin Kramer | 2e018ef | 2016-05-27 13:36:58 +0000 | [diff] [blame] | 1484 | const std::string &Variety = S.variety(); |
| 1485 | const std::string &Spelling = S.name(); |
| 1486 | const std::string &Namespace = S.nameSpace(); |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1487 | std::string EnumName; |
Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 1488 | |
Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 1489 | EnumName += (Variety + "_"); |
| 1490 | if (!Namespace.empty()) |
| 1491 | EnumName += (NormalizeNameForSpellingComparison(Namespace).str() + |
| 1492 | "_"); |
| 1493 | EnumName += NormalizeNameForSpellingComparison(Spelling); |
| 1494 | |
| 1495 | // Even if the name is not unique, this spelling index corresponds to a |
| 1496 | // particular enumerant name that we've calculated. |
| 1497 | Map[Idx] = EnumName; |
| 1498 | |
| 1499 | // Since we have been stripping underscores to avoid trampling on the |
| 1500 | // reserved namespace, we may have inadvertently created duplicate |
| 1501 | // enumerant names. These duplicates are not considered part of the |
| 1502 | // semantic spelling, and can be elided. |
| 1503 | if (Uniques.find(EnumName) != Uniques.end()) |
| 1504 | continue; |
| 1505 | |
| 1506 | Uniques.insert(EnumName); |
| 1507 | if (I != Spellings.begin()) |
| 1508 | Ret += ",\n"; |
Aaron Ballman | 9bf6b75 | 2015-03-10 17:19:18 +0000 | [diff] [blame] | 1509 | // Duplicate spellings are not considered part of the semantic spelling |
| 1510 | // enumeration, but the spelling index and semantic spelling values are |
| 1511 | // meant to be equivalent, so we must specify a concrete value for each |
| 1512 | // enumerator. |
| 1513 | Ret += " " + EnumName + " = " + llvm::utostr(Idx); |
Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 1514 | } |
| 1515 | Ret += "\n };\n\n"; |
| 1516 | return Ret; |
| 1517 | } |
| 1518 | |
| 1519 | void WriteSemanticSpellingSwitch(const std::string &VarName, |
| 1520 | const SemanticSpellingMap &Map, |
| 1521 | raw_ostream &OS) { |
| 1522 | OS << " switch (" << VarName << ") {\n default: " |
| 1523 | << "llvm_unreachable(\"Unknown spelling list index\");\n"; |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 1524 | for (const auto &I : Map) |
| 1525 | OS << " case " << I.first << ": return " << I.second << ";\n"; |
Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 1526 | OS << " }\n"; |
| 1527 | } |
| 1528 | |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 1529 | // Emits the LateParsed property for attributes. |
| 1530 | static void emitClangAttrLateParsedList(RecordKeeper &Records, raw_ostream &OS) { |
| 1531 | OS << "#if defined(CLANG_ATTR_LATE_PARSED_LIST)\n"; |
| 1532 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"); |
| 1533 | |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 1534 | for (const auto *Attr : Attrs) { |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 1535 | bool LateParsed = Attr->getValueAsBit("LateParsed"); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 1536 | |
| 1537 | if (LateParsed) { |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 1538 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 1539 | |
| 1540 | // FIXME: Handle non-GNU attributes |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 1541 | for (const auto &I : Spellings) { |
| 1542 | if (I.variety() != "GNU") |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 1543 | continue; |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 1544 | OS << ".Case(\"" << I.name() << "\", " << LateParsed << ")\n"; |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 1545 | } |
| 1546 | } |
| 1547 | } |
| 1548 | OS << "#endif // CLANG_ATTR_LATE_PARSED_LIST\n\n"; |
| 1549 | } |
| 1550 | |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1551 | static bool hasGNUorCXX11Spelling(const Record &Attribute) { |
| 1552 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attribute); |
| 1553 | for (const auto &I : Spellings) { |
| 1554 | if (I.variety() == "GNU" || I.variety() == "CXX11") |
| 1555 | return true; |
| 1556 | } |
| 1557 | return false; |
| 1558 | } |
| 1559 | |
| 1560 | namespace { |
| 1561 | |
| 1562 | struct AttributeSubjectMatchRule { |
| 1563 | const Record *MetaSubject; |
| 1564 | const Record *Constraint; |
| 1565 | |
| 1566 | AttributeSubjectMatchRule(const Record *MetaSubject, const Record *Constraint) |
| 1567 | : MetaSubject(MetaSubject), Constraint(Constraint) { |
| 1568 | assert(MetaSubject && "Missing subject"); |
| 1569 | } |
| 1570 | |
| 1571 | bool isSubRule() const { return Constraint != nullptr; } |
| 1572 | |
| 1573 | std::vector<Record *> getSubjects() const { |
| 1574 | return (Constraint ? Constraint : MetaSubject) |
| 1575 | ->getValueAsListOfDefs("Subjects"); |
| 1576 | } |
| 1577 | |
| 1578 | std::vector<Record *> getLangOpts() const { |
| 1579 | if (Constraint) { |
| 1580 | // Lookup the options in the sub-rule first, in case the sub-rule |
| 1581 | // overrides the rules options. |
| 1582 | std::vector<Record *> Opts = Constraint->getValueAsListOfDefs("LangOpts"); |
| 1583 | if (!Opts.empty()) |
| 1584 | return Opts; |
| 1585 | } |
| 1586 | return MetaSubject->getValueAsListOfDefs("LangOpts"); |
| 1587 | } |
| 1588 | |
| 1589 | // Abstract rules are used only for sub-rules |
| 1590 | bool isAbstractRule() const { return getSubjects().empty(); } |
| 1591 | |
| 1592 | std::string getName() const { |
| 1593 | return (Constraint ? Constraint : MetaSubject)->getValueAsString("Name"); |
| 1594 | } |
| 1595 | |
| 1596 | bool isNegatedSubRule() const { |
| 1597 | assert(isSubRule() && "Not a sub-rule"); |
| 1598 | return Constraint->getValueAsBit("Negated"); |
| 1599 | } |
| 1600 | |
| 1601 | std::string getSpelling() const { |
| 1602 | std::string Result = MetaSubject->getValueAsString("Name"); |
| 1603 | if (isSubRule()) { |
| 1604 | Result += '('; |
| 1605 | if (isNegatedSubRule()) |
| 1606 | Result += "unless("; |
| 1607 | Result += getName(); |
| 1608 | if (isNegatedSubRule()) |
| 1609 | Result += ')'; |
| 1610 | Result += ')'; |
| 1611 | } |
| 1612 | return Result; |
| 1613 | } |
| 1614 | |
| 1615 | std::string getEnumValueName() const { |
Craig Topper | 0064858 | 2017-05-31 19:01:22 +0000 | [diff] [blame] | 1616 | SmallString<128> Result; |
| 1617 | Result += "SubjectMatchRule_"; |
| 1618 | Result += MetaSubject->getValueAsString("Name"); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1619 | if (isSubRule()) { |
| 1620 | Result += "_"; |
| 1621 | if (isNegatedSubRule()) |
| 1622 | Result += "not_"; |
| 1623 | Result += Constraint->getValueAsString("Name"); |
| 1624 | } |
| 1625 | if (isAbstractRule()) |
| 1626 | Result += "_abstract"; |
Craig Topper | 0064858 | 2017-05-31 19:01:22 +0000 | [diff] [blame] | 1627 | return Result.str(); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | std::string getEnumValue() const { return "attr::" + getEnumValueName(); } |
| 1631 | |
| 1632 | static const char *EnumName; |
| 1633 | }; |
| 1634 | |
| 1635 | const char *AttributeSubjectMatchRule::EnumName = "attr::SubjectMatchRule"; |
| 1636 | |
| 1637 | struct PragmaClangAttributeSupport { |
| 1638 | std::vector<AttributeSubjectMatchRule> Rules; |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 1639 | |
| 1640 | class RuleOrAggregateRuleSet { |
| 1641 | std::vector<AttributeSubjectMatchRule> Rules; |
| 1642 | bool IsRule; |
| 1643 | RuleOrAggregateRuleSet(ArrayRef<AttributeSubjectMatchRule> Rules, |
| 1644 | bool IsRule) |
| 1645 | : Rules(Rules), IsRule(IsRule) {} |
| 1646 | |
| 1647 | public: |
| 1648 | bool isRule() const { return IsRule; } |
| 1649 | |
| 1650 | const AttributeSubjectMatchRule &getRule() const { |
| 1651 | assert(IsRule && "not a rule!"); |
| 1652 | return Rules[0]; |
| 1653 | } |
| 1654 | |
| 1655 | ArrayRef<AttributeSubjectMatchRule> getAggregateRuleSet() const { |
| 1656 | return Rules; |
| 1657 | } |
| 1658 | |
| 1659 | static RuleOrAggregateRuleSet |
| 1660 | getRule(const AttributeSubjectMatchRule &Rule) { |
| 1661 | return RuleOrAggregateRuleSet(Rule, /*IsRule=*/true); |
| 1662 | } |
| 1663 | static RuleOrAggregateRuleSet |
| 1664 | getAggregateRuleSet(ArrayRef<AttributeSubjectMatchRule> Rules) { |
| 1665 | return RuleOrAggregateRuleSet(Rules, /*IsRule=*/false); |
| 1666 | } |
| 1667 | }; |
| 1668 | llvm::DenseMap<const Record *, RuleOrAggregateRuleSet> SubjectsToRules; |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1669 | |
| 1670 | PragmaClangAttributeSupport(RecordKeeper &Records); |
| 1671 | |
| 1672 | bool isAttributedSupported(const Record &Attribute); |
| 1673 | |
| 1674 | void emitMatchRuleList(raw_ostream &OS); |
| 1675 | |
| 1676 | std::string generateStrictConformsTo(const Record &Attr, raw_ostream &OS); |
| 1677 | |
| 1678 | void generateParsingHelpers(raw_ostream &OS); |
| 1679 | }; |
| 1680 | |
| 1681 | } // end anonymous namespace |
| 1682 | |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 1683 | static bool doesDeclDeriveFrom(const Record *D, const Record *Base) { |
| 1684 | const Record *CurrentBase = D->getValueAsDef("Base"); |
| 1685 | if (!CurrentBase) |
| 1686 | return false; |
| 1687 | if (CurrentBase == Base) |
| 1688 | return true; |
| 1689 | return doesDeclDeriveFrom(CurrentBase, Base); |
| 1690 | } |
| 1691 | |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1692 | PragmaClangAttributeSupport::PragmaClangAttributeSupport( |
| 1693 | RecordKeeper &Records) { |
| 1694 | std::vector<Record *> MetaSubjects = |
| 1695 | Records.getAllDerivedDefinitions("AttrSubjectMatcherRule"); |
| 1696 | auto MapFromSubjectsToRules = [this](const Record *SubjectContainer, |
| 1697 | const Record *MetaSubject, |
Saleem Abdulrasool | be4773c | 2017-05-01 00:26:59 +0000 | [diff] [blame] | 1698 | const Record *Constraint) { |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1699 | Rules.emplace_back(MetaSubject, Constraint); |
| 1700 | std::vector<Record *> ApplicableSubjects = |
| 1701 | SubjectContainer->getValueAsListOfDefs("Subjects"); |
| 1702 | for (const auto *Subject : ApplicableSubjects) { |
| 1703 | bool Inserted = |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 1704 | SubjectsToRules |
| 1705 | .try_emplace(Subject, RuleOrAggregateRuleSet::getRule( |
| 1706 | AttributeSubjectMatchRule(MetaSubject, |
| 1707 | Constraint))) |
| 1708 | .second; |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1709 | if (!Inserted) { |
| 1710 | PrintFatalError("Attribute subject match rules should not represent" |
| 1711 | "same attribute subjects."); |
| 1712 | } |
| 1713 | } |
| 1714 | }; |
| 1715 | for (const auto *MetaSubject : MetaSubjects) { |
Saleem Abdulrasool | be4773c | 2017-05-01 00:26:59 +0000 | [diff] [blame] | 1716 | MapFromSubjectsToRules(MetaSubject, MetaSubject, /*Constraints=*/nullptr); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1717 | std::vector<Record *> Constraints = |
| 1718 | MetaSubject->getValueAsListOfDefs("Constraints"); |
| 1719 | for (const auto *Constraint : Constraints) |
| 1720 | MapFromSubjectsToRules(Constraint, MetaSubject, Constraint); |
| 1721 | } |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 1722 | |
| 1723 | std::vector<Record *> Aggregates = |
| 1724 | Records.getAllDerivedDefinitions("AttrSubjectMatcherAggregateRule"); |
| 1725 | std::vector<Record *> DeclNodes = Records.getAllDerivedDefinitions("DDecl"); |
| 1726 | for (const auto *Aggregate : Aggregates) { |
| 1727 | Record *SubjectDecl = Aggregate->getValueAsDef("Subject"); |
| 1728 | |
| 1729 | // Gather sub-classes of the aggregate subject that act as attribute |
| 1730 | // subject rules. |
| 1731 | std::vector<AttributeSubjectMatchRule> Rules; |
| 1732 | for (const auto *D : DeclNodes) { |
| 1733 | if (doesDeclDeriveFrom(D, SubjectDecl)) { |
| 1734 | auto It = SubjectsToRules.find(D); |
| 1735 | if (It == SubjectsToRules.end()) |
| 1736 | continue; |
| 1737 | if (!It->second.isRule() || It->second.getRule().isSubRule()) |
| 1738 | continue; // Assume that the rule will be included as well. |
| 1739 | Rules.push_back(It->second.getRule()); |
| 1740 | } |
| 1741 | } |
| 1742 | |
| 1743 | bool Inserted = |
| 1744 | SubjectsToRules |
| 1745 | .try_emplace(SubjectDecl, |
| 1746 | RuleOrAggregateRuleSet::getAggregateRuleSet(Rules)) |
| 1747 | .second; |
| 1748 | if (!Inserted) { |
| 1749 | PrintFatalError("Attribute subject match rules should not represent" |
| 1750 | "same attribute subjects."); |
| 1751 | } |
| 1752 | } |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | static PragmaClangAttributeSupport & |
| 1756 | getPragmaAttributeSupport(RecordKeeper &Records) { |
| 1757 | static PragmaClangAttributeSupport Instance(Records); |
| 1758 | return Instance; |
| 1759 | } |
| 1760 | |
| 1761 | void PragmaClangAttributeSupport::emitMatchRuleList(raw_ostream &OS) { |
| 1762 | OS << "#ifndef ATTR_MATCH_SUB_RULE\n"; |
| 1763 | OS << "#define ATTR_MATCH_SUB_RULE(Value, Spelling, IsAbstract, Parent, " |
| 1764 | "IsNegated) " |
| 1765 | << "ATTR_MATCH_RULE(Value, Spelling, IsAbstract)\n"; |
| 1766 | OS << "#endif\n"; |
| 1767 | for (const auto &Rule : Rules) { |
| 1768 | OS << (Rule.isSubRule() ? "ATTR_MATCH_SUB_RULE" : "ATTR_MATCH_RULE") << '('; |
| 1769 | OS << Rule.getEnumValueName() << ", \"" << Rule.getSpelling() << "\", " |
| 1770 | << Rule.isAbstractRule(); |
| 1771 | if (Rule.isSubRule()) |
| 1772 | OS << ", " |
| 1773 | << AttributeSubjectMatchRule(Rule.MetaSubject, nullptr).getEnumValue() |
| 1774 | << ", " << Rule.isNegatedSubRule(); |
| 1775 | OS << ")\n"; |
| 1776 | } |
| 1777 | OS << "#undef ATTR_MATCH_SUB_RULE\n"; |
| 1778 | } |
| 1779 | |
| 1780 | bool PragmaClangAttributeSupport::isAttributedSupported( |
| 1781 | const Record &Attribute) { |
| 1782 | if (Attribute.getValueAsBit("ForcePragmaAttributeSupport")) |
| 1783 | return true; |
| 1784 | // Opt-out rules: |
| 1785 | // FIXME: The documentation check should be moved before |
| 1786 | // the ForcePragmaAttributeSupport check after annotate is documented. |
| 1787 | // No documentation present. |
| 1788 | if (Attribute.isValueUnset("Documentation")) |
| 1789 | return false; |
| 1790 | std::vector<Record *> Docs = Attribute.getValueAsListOfDefs("Documentation"); |
| 1791 | if (Docs.empty()) |
| 1792 | return false; |
| 1793 | if (Docs.size() == 1 && Docs[0]->getName() == "Undocumented") |
| 1794 | return false; |
| 1795 | // An attribute requires delayed parsing (LateParsed is on) |
| 1796 | if (Attribute.getValueAsBit("LateParsed")) |
| 1797 | return false; |
| 1798 | // An attribute has no GNU/CXX11 spelling |
| 1799 | if (!hasGNUorCXX11Spelling(Attribute)) |
| 1800 | return false; |
| 1801 | // An attribute subject list has a subject that isn't covered by one of the |
| 1802 | // subject match rules or has no subjects at all. |
| 1803 | if (Attribute.isValueUnset("Subjects")) |
| 1804 | return false; |
| 1805 | const Record *SubjectObj = Attribute.getValueAsDef("Subjects"); |
| 1806 | std::vector<Record *> Subjects = SubjectObj->getValueAsListOfDefs("Subjects"); |
| 1807 | if (Subjects.empty()) |
| 1808 | return false; |
| 1809 | for (const auto *Subject : Subjects) { |
| 1810 | if (SubjectsToRules.find(Subject) == SubjectsToRules.end()) |
| 1811 | return false; |
| 1812 | } |
| 1813 | return true; |
| 1814 | } |
| 1815 | |
| 1816 | std::string |
| 1817 | PragmaClangAttributeSupport::generateStrictConformsTo(const Record &Attr, |
| 1818 | raw_ostream &OS) { |
| 1819 | if (!isAttributedSupported(Attr)) |
| 1820 | return "nullptr"; |
| 1821 | // Generate a function that constructs a set of matching rules that describe |
| 1822 | // to which declarations the attribute should apply to. |
| 1823 | std::string FnName = "matchRulesFor" + Attr.getName().str(); |
| 1824 | std::stringstream SS; |
| 1825 | SS << "static void " << FnName << "(llvm::SmallVectorImpl<std::pair<" |
| 1826 | << AttributeSubjectMatchRule::EnumName |
| 1827 | << ", bool>> &MatchRules, const LangOptions &LangOpts) {\n"; |
| 1828 | if (Attr.isValueUnset("Subjects")) { |
| 1829 | SS << "}\n\n"; |
| 1830 | OS << SS.str(); |
| 1831 | return FnName; |
| 1832 | } |
| 1833 | const Record *SubjectObj = Attr.getValueAsDef("Subjects"); |
| 1834 | std::vector<Record *> Subjects = SubjectObj->getValueAsListOfDefs("Subjects"); |
| 1835 | for (const auto *Subject : Subjects) { |
| 1836 | auto It = SubjectsToRules.find(Subject); |
| 1837 | assert(It != SubjectsToRules.end() && |
| 1838 | "This attribute is unsupported by #pragma clang attribute"); |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 1839 | for (const auto &Rule : It->getSecond().getAggregateRuleSet()) { |
| 1840 | // The rule might be language specific, so only subtract it from the given |
| 1841 | // rules if the specific language options are specified. |
| 1842 | std::vector<Record *> LangOpts = Rule.getLangOpts(); |
| 1843 | SS << " MatchRules.push_back(std::make_pair(" << Rule.getEnumValue() |
| 1844 | << ", /*IsSupported=*/"; |
| 1845 | if (!LangOpts.empty()) { |
| 1846 | for (auto I = LangOpts.begin(), E = LangOpts.end(); I != E; ++I) { |
| 1847 | std::string Part = (*I)->getValueAsString("Name"); |
| 1848 | if ((*I)->getValueAsBit("Negated")) |
| 1849 | SS << "!"; |
| 1850 | SS << "LangOpts." + Part; |
| 1851 | if (I + 1 != E) |
| 1852 | SS << " || "; |
| 1853 | } |
| 1854 | } else |
| 1855 | SS << "true"; |
| 1856 | SS << "));\n"; |
| 1857 | } |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1858 | } |
| 1859 | SS << "}\n\n"; |
| 1860 | OS << SS.str(); |
| 1861 | return FnName; |
| 1862 | } |
| 1863 | |
| 1864 | void PragmaClangAttributeSupport::generateParsingHelpers(raw_ostream &OS) { |
| 1865 | // Generate routines that check the names of sub-rules. |
| 1866 | OS << "Optional<attr::SubjectMatchRule> " |
| 1867 | "defaultIsAttributeSubjectMatchSubRuleFor(StringRef, bool) {\n"; |
| 1868 | OS << " return None;\n"; |
| 1869 | OS << "}\n\n"; |
| 1870 | |
| 1871 | std::map<const Record *, std::vector<AttributeSubjectMatchRule>> |
| 1872 | SubMatchRules; |
| 1873 | for (const auto &Rule : Rules) { |
| 1874 | if (!Rule.isSubRule()) |
| 1875 | continue; |
| 1876 | SubMatchRules[Rule.MetaSubject].push_back(Rule); |
| 1877 | } |
| 1878 | |
| 1879 | for (const auto &SubMatchRule : SubMatchRules) { |
| 1880 | OS << "Optional<attr::SubjectMatchRule> isAttributeSubjectMatchSubRuleFor_" |
| 1881 | << SubMatchRule.first->getValueAsString("Name") |
| 1882 | << "(StringRef Name, bool IsUnless) {\n"; |
| 1883 | OS << " if (IsUnless)\n"; |
| 1884 | OS << " return " |
| 1885 | "llvm::StringSwitch<Optional<attr::SubjectMatchRule>>(Name).\n"; |
| 1886 | for (const auto &Rule : SubMatchRule.second) { |
| 1887 | if (Rule.isNegatedSubRule()) |
| 1888 | OS << " Case(\"" << Rule.getName() << "\", " << Rule.getEnumValue() |
| 1889 | << ").\n"; |
| 1890 | } |
| 1891 | OS << " Default(None);\n"; |
| 1892 | OS << " return " |
| 1893 | "llvm::StringSwitch<Optional<attr::SubjectMatchRule>>(Name).\n"; |
| 1894 | for (const auto &Rule : SubMatchRule.second) { |
| 1895 | if (!Rule.isNegatedSubRule()) |
| 1896 | OS << " Case(\"" << Rule.getName() << "\", " << Rule.getEnumValue() |
| 1897 | << ").\n"; |
| 1898 | } |
| 1899 | OS << " Default(None);\n"; |
| 1900 | OS << "}\n\n"; |
| 1901 | } |
| 1902 | |
| 1903 | // Generate the function that checks for the top-level rules. |
| 1904 | OS << "std::pair<Optional<attr::SubjectMatchRule>, " |
| 1905 | "Optional<attr::SubjectMatchRule> (*)(StringRef, " |
| 1906 | "bool)> isAttributeSubjectMatchRule(StringRef Name) {\n"; |
| 1907 | OS << " return " |
| 1908 | "llvm::StringSwitch<std::pair<Optional<attr::SubjectMatchRule>, " |
| 1909 | "Optional<attr::SubjectMatchRule> (*) (StringRef, " |
| 1910 | "bool)>>(Name).\n"; |
| 1911 | for (const auto &Rule : Rules) { |
| 1912 | if (Rule.isSubRule()) |
| 1913 | continue; |
| 1914 | std::string SubRuleFunction; |
| 1915 | if (SubMatchRules.count(Rule.MetaSubject)) |
| 1916 | SubRuleFunction = "isAttributeSubjectMatchSubRuleFor_" + Rule.getName(); |
| 1917 | else |
| 1918 | SubRuleFunction = "defaultIsAttributeSubjectMatchSubRuleFor"; |
| 1919 | OS << " Case(\"" << Rule.getName() << "\", std::make_pair(" |
| 1920 | << Rule.getEnumValue() << ", " << SubRuleFunction << ")).\n"; |
| 1921 | } |
| 1922 | OS << " Default(std::make_pair(None, " |
| 1923 | "defaultIsAttributeSubjectMatchSubRuleFor));\n"; |
| 1924 | OS << "}\n\n"; |
| 1925 | |
| 1926 | // Generate the function that checks for the submatch rules. |
| 1927 | OS << "const char *validAttributeSubjectMatchSubRules(" |
| 1928 | << AttributeSubjectMatchRule::EnumName << " Rule) {\n"; |
| 1929 | OS << " switch (Rule) {\n"; |
| 1930 | for (const auto &SubMatchRule : SubMatchRules) { |
| 1931 | OS << " case " |
| 1932 | << AttributeSubjectMatchRule(SubMatchRule.first, nullptr).getEnumValue() |
| 1933 | << ":\n"; |
| 1934 | OS << " return \"'"; |
| 1935 | bool IsFirst = true; |
| 1936 | for (const auto &Rule : SubMatchRule.second) { |
| 1937 | if (!IsFirst) |
| 1938 | OS << ", '"; |
| 1939 | IsFirst = false; |
| 1940 | if (Rule.isNegatedSubRule()) |
| 1941 | OS << "unless("; |
| 1942 | OS << Rule.getName(); |
| 1943 | if (Rule.isNegatedSubRule()) |
| 1944 | OS << ')'; |
| 1945 | OS << "'"; |
| 1946 | } |
| 1947 | OS << "\";\n"; |
| 1948 | } |
| 1949 | OS << " default: return nullptr;\n"; |
| 1950 | OS << " }\n"; |
| 1951 | OS << "}\n\n"; |
| 1952 | } |
| 1953 | |
George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 1954 | template <typename Fn> |
| 1955 | static void forEachUniqueSpelling(const Record &Attr, Fn &&F) { |
| 1956 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr); |
| 1957 | SmallDenseSet<StringRef, 8> Seen; |
| 1958 | for (const FlattenedSpelling &S : Spellings) { |
| 1959 | if (Seen.insert(S.name()).second) |
| 1960 | F(S); |
| 1961 | } |
| 1962 | } |
| 1963 | |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 1964 | /// \brief Emits the first-argument-is-type property for attributes. |
| 1965 | static void emitClangAttrTypeArgList(RecordKeeper &Records, raw_ostream &OS) { |
| 1966 | OS << "#if defined(CLANG_ATTR_TYPE_ARG_LIST)\n"; |
| 1967 | std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); |
| 1968 | |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 1969 | for (const auto *Attr : Attrs) { |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 1970 | // Determine whether the first argument is a type. |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 1971 | std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args"); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 1972 | if (Args.empty()) |
| 1973 | continue; |
| 1974 | |
Craig Topper | 2576124 | 2016-01-18 19:52:54 +0000 | [diff] [blame] | 1975 | if (Args[0]->getSuperClasses().back().first->getName() != "TypeArgument") |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 1976 | continue; |
| 1977 | |
| 1978 | // All these spellings take a single type argument. |
George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 1979 | forEachUniqueSpelling(*Attr, [&](const FlattenedSpelling &S) { |
| 1980 | OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n"; |
| 1981 | }); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 1982 | } |
| 1983 | OS << "#endif // CLANG_ATTR_TYPE_ARG_LIST\n\n"; |
| 1984 | } |
| 1985 | |
| 1986 | /// \brief Emits the parse-arguments-in-unevaluated-context property for |
| 1987 | /// attributes. |
| 1988 | static void emitClangAttrArgContextList(RecordKeeper &Records, raw_ostream &OS) { |
| 1989 | OS << "#if defined(CLANG_ATTR_ARG_CONTEXT_LIST)\n"; |
| 1990 | ParsedAttrMap Attrs = getParsedAttrList(Records); |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 1991 | for (const auto &I : Attrs) { |
| 1992 | const Record &Attr = *I.second; |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 1993 | |
| 1994 | if (!Attr.getValueAsBit("ParseArgumentsAsUnevaluated")) |
| 1995 | continue; |
| 1996 | |
| 1997 | // All these spellings take are parsed unevaluated. |
George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 1998 | forEachUniqueSpelling(Attr, [&](const FlattenedSpelling &S) { |
| 1999 | OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n"; |
| 2000 | }); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 2001 | } |
| 2002 | OS << "#endif // CLANG_ATTR_ARG_CONTEXT_LIST\n\n"; |
| 2003 | } |
| 2004 | |
| 2005 | static bool isIdentifierArgument(Record *Arg) { |
| 2006 | return !Arg->getSuperClasses().empty() && |
Craig Topper | 2576124 | 2016-01-18 19:52:54 +0000 | [diff] [blame] | 2007 | llvm::StringSwitch<bool>(Arg->getSuperClasses().back().first->getName()) |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 2008 | .Case("IdentifierArgument", true) |
| 2009 | .Case("EnumArgument", true) |
Aaron Ballman | 55ef151 | 2014-12-19 16:42:04 +0000 | [diff] [blame] | 2010 | .Case("VariadicEnumArgument", true) |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 2011 | .Default(false); |
| 2012 | } |
| 2013 | |
| 2014 | // Emits the first-argument-is-identifier property for attributes. |
| 2015 | static void emitClangAttrIdentifierArgList(RecordKeeper &Records, raw_ostream &OS) { |
| 2016 | OS << "#if defined(CLANG_ATTR_IDENTIFIER_ARG_LIST)\n"; |
| 2017 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"); |
| 2018 | |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2019 | for (const auto *Attr : Attrs) { |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 2020 | // Determine whether the first argument is an identifier. |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 2021 | std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args"); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 2022 | if (Args.empty() || !isIdentifierArgument(Args[0])) |
| 2023 | continue; |
| 2024 | |
| 2025 | // All these spellings take an identifier argument. |
George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 2026 | forEachUniqueSpelling(*Attr, [&](const FlattenedSpelling &S) { |
| 2027 | OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n"; |
| 2028 | }); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 2029 | } |
| 2030 | OS << "#endif // CLANG_ATTR_IDENTIFIER_ARG_LIST\n\n"; |
| 2031 | } |
| 2032 | |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 2033 | namespace clang { |
| 2034 | |
| 2035 | // Emits the class definitions for attributes. |
| 2036 | void EmitClangAttrClass(RecordKeeper &Records, raw_ostream &OS) { |
Dmitri Gribenko | 6b11fca | 2013-01-30 21:54:20 +0000 | [diff] [blame] | 2037 | emitSourceFileHeader("Attribute classes' definitions", OS); |
| 2038 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2039 | OS << "#ifndef LLVM_CLANG_ATTR_CLASSES_INC\n"; |
| 2040 | OS << "#define LLVM_CLANG_ATTR_CLASSES_INC\n\n"; |
| 2041 | |
| 2042 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"); |
| 2043 | |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2044 | for (const auto *Attr : Attrs) { |
| 2045 | const Record &R = *Attr; |
Aaron Ballman | 06bd44b | 2014-02-17 18:23:02 +0000 | [diff] [blame] | 2046 | |
| 2047 | // FIXME: Currently, documentation is generated as-needed due to the fact |
| 2048 | // that there is no way to allow a generated project "reach into" the docs |
| 2049 | // directory (for instance, it may be an out-of-tree build). However, we want |
| 2050 | // to ensure that every attribute has a Documentation field, and produce an |
| 2051 | // error if it has been neglected. Otherwise, the on-demand generation which |
| 2052 | // happens server-side will fail. This code is ensuring that functionality, |
| 2053 | // even though this Emitter doesn't technically need the documentation. |
| 2054 | // When attribute documentation can be generated as part of the build |
| 2055 | // itself, this code can be removed. |
| 2056 | (void)R.getValueAsListOfDefs("Documentation"); |
Douglas Gregor | b2daf84 | 2012-05-02 15:56:52 +0000 | [diff] [blame] | 2057 | |
| 2058 | if (!R.getValueAsBit("ASTNode")) |
| 2059 | continue; |
| 2060 | |
Craig Topper | 2576124 | 2016-01-18 19:52:54 +0000 | [diff] [blame] | 2061 | ArrayRef<std::pair<Record *, SMRange>> Supers = R.getSuperClasses(); |
Aaron Ballman | 0979e9e | 2013-07-30 01:44:15 +0000 | [diff] [blame] | 2062 | assert(!Supers.empty() && "Forgot to specify a superclass for the attr"); |
Aaron Ballman | 0979e9e | 2013-07-30 01:44:15 +0000 | [diff] [blame] | 2063 | std::string SuperName; |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2064 | for (const auto &Super : llvm::reverse(Supers)) { |
Craig Topper | 2576124 | 2016-01-18 19:52:54 +0000 | [diff] [blame] | 2065 | const Record *R = Super.first; |
| 2066 | if (R->getName() != "TargetSpecificAttr" && SuperName.empty()) |
| 2067 | SuperName = R->getName(); |
Aaron Ballman | 0979e9e | 2013-07-30 01:44:15 +0000 | [diff] [blame] | 2068 | } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2069 | |
| 2070 | OS << "class " << R.getName() << "Attr : public " << SuperName << " {\n"; |
| 2071 | |
| 2072 | std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args"); |
Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2073 | std::vector<std::unique_ptr<Argument>> Args; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2074 | Args.reserve(ArgRecords.size()); |
| 2075 | |
John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 2076 | bool HasOptArg = false; |
| 2077 | bool HasFakeArg = false; |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2078 | for (const auto *ArgRecord : ArgRecords) { |
Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2079 | Args.emplace_back(createArgument(*ArgRecord, R.getName())); |
| 2080 | Args.back()->writeDeclarations(OS); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2081 | OS << "\n\n"; |
John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 2082 | |
| 2083 | // For these purposes, fake takes priority over optional. |
| 2084 | if (Args.back()->isFake()) { |
| 2085 | HasFakeArg = true; |
| 2086 | } else if (Args.back()->isOptional()) { |
| 2087 | HasOptArg = true; |
| 2088 | } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 2091 | OS << "public:\n"; |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2092 | |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 2093 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R); |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2094 | |
| 2095 | // If there are zero or one spellings, all spelling-related functionality |
| 2096 | // can be elided. If all of the spellings share the same name, the spelling |
| 2097 | // functionality can also be elided. |
| 2098 | bool ElideSpelling = (Spellings.size() <= 1) || |
| 2099 | SpellingNamesAreCommon(Spellings); |
| 2100 | |
Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 2101 | // This maps spelling index values to semantic Spelling enumerants. |
| 2102 | SemanticSpellingMap SemanticToSyntacticMap; |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2103 | |
Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 2104 | if (!ElideSpelling) |
| 2105 | OS << CreateSemanticSpellings(Spellings, SemanticToSyntacticMap); |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2106 | |
John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 2107 | // Emit CreateImplicit factory methods. |
| 2108 | auto emitCreateImplicit = [&](bool emitFake) { |
| 2109 | OS << " static " << R.getName() << "Attr *CreateImplicit("; |
| 2110 | OS << "ASTContext &Ctx"; |
| 2111 | if (!ElideSpelling) |
| 2112 | OS << ", Spelling S"; |
| 2113 | for (auto const &ai : Args) { |
| 2114 | if (ai->isFake() && !emitFake) continue; |
| 2115 | OS << ", "; |
| 2116 | ai->writeCtorParameters(OS); |
| 2117 | } |
| 2118 | OS << ", SourceRange Loc = SourceRange()"; |
| 2119 | OS << ") {\n"; |
Eugene Zelenko | 5f02b77 | 2015-12-08 18:49:01 +0000 | [diff] [blame] | 2120 | OS << " auto *A = new (Ctx) " << R.getName(); |
John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 2121 | OS << "Attr(Loc, Ctx, "; |
| 2122 | for (auto const &ai : Args) { |
| 2123 | if (ai->isFake() && !emitFake) continue; |
| 2124 | ai->writeImplicitCtorArgs(OS); |
| 2125 | OS << ", "; |
| 2126 | } |
| 2127 | OS << (ElideSpelling ? "0" : "S") << ");\n"; |
| 2128 | OS << " A->setImplicit(true);\n"; |
| 2129 | OS << " return A;\n }\n\n"; |
| 2130 | }; |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2131 | |
John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 2132 | // Emit a CreateImplicit that takes all the arguments. |
| 2133 | emitCreateImplicit(true); |
| 2134 | |
| 2135 | // Emit a CreateImplicit that takes all the non-fake arguments. |
| 2136 | if (HasFakeArg) { |
| 2137 | emitCreateImplicit(false); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2138 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2139 | |
John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 2140 | // Emit constructors. |
| 2141 | auto emitCtor = [&](bool emitOpt, bool emitFake) { |
| 2142 | auto shouldEmitArg = [=](const std::unique_ptr<Argument> &arg) { |
| 2143 | if (arg->isFake()) return emitFake; |
| 2144 | if (arg->isOptional()) return emitOpt; |
| 2145 | return true; |
| 2146 | }; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2147 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 2148 | OS << " " << R.getName() << "Attr(SourceRange R, ASTContext &Ctx\n"; |
Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2149 | for (auto const &ai : Args) { |
John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 2150 | if (!shouldEmitArg(ai)) continue; |
| 2151 | OS << " , "; |
| 2152 | ai->writeCtorParameters(OS); |
| 2153 | OS << "\n"; |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
| 2156 | OS << " , "; |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2157 | OS << "unsigned SI\n"; |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 2158 | |
| 2159 | OS << " )\n"; |
Benjamin Kramer | 845e32c | 2015-03-19 16:06:49 +0000 | [diff] [blame] | 2160 | OS << " : " << SuperName << "(attr::" << R.getName() << ", R, SI, " |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 2161 | << ( R.getValueAsBit("LateParsed") ? "true" : "false" ) << ", " |
| 2162 | << ( R.getValueAsBit("DuplicatesAllowedWhileMerging") ? "true" : "false" ) << ")\n"; |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 2163 | |
Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2164 | for (auto const &ai : Args) { |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 2165 | OS << " , "; |
John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 2166 | if (!shouldEmitArg(ai)) { |
| 2167 | ai->writeCtorDefaultInitializers(OS); |
| 2168 | } else { |
| 2169 | ai->writeCtorInitializers(OS); |
| 2170 | } |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 2171 | OS << "\n"; |
| 2172 | } |
| 2173 | |
| 2174 | OS << " {\n"; |
| 2175 | |
Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2176 | for (auto const &ai : Args) { |
John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 2177 | if (!shouldEmitArg(ai)) continue; |
| 2178 | ai->writeCtorBody(OS); |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 2179 | } |
| 2180 | OS << " }\n\n"; |
John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 2181 | }; |
| 2182 | |
| 2183 | // Emit a constructor that includes all the arguments. |
| 2184 | // This is necessary for cloning. |
| 2185 | emitCtor(true, true); |
| 2186 | |
| 2187 | // Emit a constructor that takes all the non-fake arguments. |
| 2188 | if (HasFakeArg) { |
| 2189 | emitCtor(true, false); |
| 2190 | } |
| 2191 | |
| 2192 | // Emit a constructor that takes all the non-fake, non-optional arguments. |
| 2193 | if (HasOptArg) { |
| 2194 | emitCtor(false, false); |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 2195 | } |
| 2196 | |
Benjamin Kramer | 845e32c | 2015-03-19 16:06:49 +0000 | [diff] [blame] | 2197 | OS << " " << R.getName() << "Attr *clone(ASTContext &C) const;\n"; |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 2198 | OS << " void printPretty(raw_ostream &OS,\n" |
Benjamin Kramer | 845e32c | 2015-03-19 16:06:49 +0000 | [diff] [blame] | 2199 | << " const PrintingPolicy &Policy) const;\n"; |
| 2200 | OS << " const char *getSpelling() const;\n"; |
Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 2201 | |
| 2202 | if (!ElideSpelling) { |
| 2203 | assert(!SemanticToSyntacticMap.empty() && "Empty semantic mapping list"); |
| 2204 | OS << " Spelling getSemanticSpelling() const {\n"; |
| 2205 | WriteSemanticSpellingSwitch("SpellingListIndex", SemanticToSyntacticMap, |
| 2206 | OS); |
| 2207 | OS << " }\n"; |
| 2208 | } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2209 | |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2210 | writeAttrAccessorDefinition(R, OS); |
| 2211 | |
Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2212 | for (auto const &ai : Args) { |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 2213 | ai->writeAccessors(OS); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2214 | OS << "\n\n"; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2215 | |
John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 2216 | // Don't write conversion routines for fake arguments. |
| 2217 | if (ai->isFake()) continue; |
| 2218 | |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 2219 | if (ai->isEnumArg()) |
Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2220 | static_cast<const EnumArgument *>(ai.get())->writeConversion(OS); |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 2221 | else if (ai->isVariadicEnumArg()) |
Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2222 | static_cast<const VariadicEnumArgument *>(ai.get()) |
| 2223 | ->writeConversion(OS); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2224 | } |
| 2225 | |
Jakob Stoklund Olesen | 6f2288b6 | 2012-01-13 04:57:47 +0000 | [diff] [blame] | 2226 | OS << R.getValueAsString("AdditionalMembers"); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2227 | OS << "\n\n"; |
| 2228 | |
| 2229 | OS << " static bool classof(const Attr *A) { return A->getKind() == " |
| 2230 | << "attr::" << R.getName() << "; }\n"; |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 2231 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2232 | OS << "};\n\n"; |
| 2233 | } |
| 2234 | |
Eugene Zelenko | 5f02b77 | 2015-12-08 18:49:01 +0000 | [diff] [blame] | 2235 | OS << "#endif // LLVM_CLANG_ATTR_CLASSES_INC\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2236 | } |
| 2237 | |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 2238 | // Emits the class method definitions for attributes. |
| 2239 | void EmitClangAttrImpl(RecordKeeper &Records, raw_ostream &OS) { |
Dmitri Gribenko | 6b11fca | 2013-01-30 21:54:20 +0000 | [diff] [blame] | 2240 | emitSourceFileHeader("Attribute classes' member function definitions", OS); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2241 | |
| 2242 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2243 | |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2244 | for (auto *Attr : Attrs) { |
| 2245 | Record &R = *Attr; |
Douglas Gregor | b2daf84 | 2012-05-02 15:56:52 +0000 | [diff] [blame] | 2246 | |
| 2247 | if (!R.getValueAsBit("ASTNode")) |
| 2248 | continue; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2249 | |
Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2250 | std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args"); |
| 2251 | std::vector<std::unique_ptr<Argument>> Args; |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2252 | for (const auto *Arg : ArgRecords) |
| 2253 | Args.emplace_back(createArgument(*Arg, R.getName())); |
Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2254 | |
| 2255 | for (auto const &ai : Args) |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 2256 | ai->writeAccessorDefinitions(OS); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2257 | |
| 2258 | OS << R.getName() << "Attr *" << R.getName() |
| 2259 | << "Attr::clone(ASTContext &C) const {\n"; |
Hans Wennborg | 613807b | 2014-05-31 01:30:30 +0000 | [diff] [blame] | 2260 | OS << " auto *A = new (C) " << R.getName() << "Attr(getLocation(), C"; |
Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2261 | for (auto const &ai : Args) { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2262 | OS << ", "; |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 2263 | ai->writeCloneArgs(OS); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2264 | } |
Hans Wennborg | 613807b | 2014-05-31 01:30:30 +0000 | [diff] [blame] | 2265 | OS << ", getSpellingListIndex());\n"; |
| 2266 | OS << " A->Inherited = Inherited;\n"; |
| 2267 | OS << " A->IsPackExpansion = IsPackExpansion;\n"; |
| 2268 | OS << " A->Implicit = Implicit;\n"; |
| 2269 | OS << " return A;\n}\n\n"; |
Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 2270 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2271 | writePrettyPrintFunction(R, Args, OS); |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 2272 | writeGetSpellingFunction(R, OS); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2273 | } |
Benjamin Kramer | 845e32c | 2015-03-19 16:06:49 +0000 | [diff] [blame] | 2274 | |
| 2275 | // Instead of relying on virtual dispatch we just create a huge dispatch |
| 2276 | // switch. This is both smaller and faster than virtual functions. |
| 2277 | auto EmitFunc = [&](const char *Method) { |
| 2278 | OS << " switch (getKind()) {\n"; |
| 2279 | for (const auto *Attr : Attrs) { |
| 2280 | const Record &R = *Attr; |
| 2281 | if (!R.getValueAsBit("ASTNode")) |
| 2282 | continue; |
| 2283 | |
| 2284 | OS << " case attr::" << R.getName() << ":\n"; |
| 2285 | OS << " return cast<" << R.getName() << "Attr>(this)->" << Method |
| 2286 | << ";\n"; |
| 2287 | } |
Benjamin Kramer | 845e32c | 2015-03-19 16:06:49 +0000 | [diff] [blame] | 2288 | OS << " }\n"; |
| 2289 | OS << " llvm_unreachable(\"Unexpected attribute kind!\");\n"; |
| 2290 | OS << "}\n\n"; |
| 2291 | }; |
| 2292 | |
| 2293 | OS << "const char *Attr::getSpelling() const {\n"; |
| 2294 | EmitFunc("getSpelling()"); |
| 2295 | |
| 2296 | OS << "Attr *Attr::clone(ASTContext &C) const {\n"; |
| 2297 | EmitFunc("clone(C)"); |
| 2298 | |
| 2299 | OS << "void Attr::printPretty(raw_ostream &OS, " |
| 2300 | "const PrintingPolicy &Policy) const {\n"; |
| 2301 | EmitFunc("printPretty(OS, Policy)"); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 2304 | } // end namespace clang |
| 2305 | |
John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2306 | static void emitAttrList(raw_ostream &OS, StringRef Class, |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2307 | const std::vector<Record*> &AttrList) { |
John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2308 | for (auto Cur : AttrList) { |
| 2309 | OS << Class << "(" << Cur->getName() << ")\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2310 | } |
| 2311 | } |
| 2312 | |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 2313 | // Determines if an attribute has a Pragma spelling. |
| 2314 | static bool AttrHasPragmaSpelling(const Record *R) { |
| 2315 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R); |
George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 2316 | return llvm::find_if(Spellings, [](const FlattenedSpelling &S) { |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 2317 | return S.variety() == "Pragma"; |
| 2318 | }) != Spellings.end(); |
| 2319 | } |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 2320 | |
John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2321 | namespace { |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 2322 | |
John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2323 | struct AttrClassDescriptor { |
| 2324 | const char * const MacroName; |
| 2325 | const char * const TableGenName; |
| 2326 | }; |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 2327 | |
| 2328 | } // end anonymous namespace |
John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2329 | |
| 2330 | static const AttrClassDescriptor AttrClassDescriptors[] = { |
| 2331 | { "ATTR", "Attr" }, |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 2332 | { "STMT_ATTR", "StmtAttr" }, |
John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2333 | { "INHERITABLE_ATTR", "InheritableAttr" }, |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 2334 | { "INHERITABLE_PARAM_ATTR", "InheritableParamAttr" }, |
| 2335 | { "PARAMETER_ABI_ATTR", "ParameterABIAttr" } |
John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2336 | }; |
| 2337 | |
| 2338 | static void emitDefaultDefine(raw_ostream &OS, StringRef name, |
| 2339 | const char *superName) { |
| 2340 | OS << "#ifndef " << name << "\n"; |
| 2341 | OS << "#define " << name << "(NAME) "; |
| 2342 | if (superName) OS << superName << "(NAME)"; |
| 2343 | OS << "\n#endif\n\n"; |
| 2344 | } |
| 2345 | |
| 2346 | namespace { |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 2347 | |
John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2348 | /// A class of attributes. |
| 2349 | struct AttrClass { |
| 2350 | const AttrClassDescriptor &Descriptor; |
| 2351 | Record *TheRecord; |
| 2352 | AttrClass *SuperClass = nullptr; |
| 2353 | std::vector<AttrClass*> SubClasses; |
| 2354 | std::vector<Record*> Attrs; |
| 2355 | |
| 2356 | AttrClass(const AttrClassDescriptor &Descriptor, Record *R) |
| 2357 | : Descriptor(Descriptor), TheRecord(R) {} |
| 2358 | |
| 2359 | void emitDefaultDefines(raw_ostream &OS) const { |
| 2360 | // Default the macro unless this is a root class (i.e. Attr). |
| 2361 | if (SuperClass) { |
| 2362 | emitDefaultDefine(OS, Descriptor.MacroName, |
| 2363 | SuperClass->Descriptor.MacroName); |
| 2364 | } |
| 2365 | } |
| 2366 | |
| 2367 | void emitUndefs(raw_ostream &OS) const { |
| 2368 | OS << "#undef " << Descriptor.MacroName << "\n"; |
| 2369 | } |
| 2370 | |
| 2371 | void emitAttrList(raw_ostream &OS) const { |
| 2372 | for (auto SubClass : SubClasses) { |
| 2373 | SubClass->emitAttrList(OS); |
| 2374 | } |
| 2375 | |
| 2376 | ::emitAttrList(OS, Descriptor.MacroName, Attrs); |
| 2377 | } |
| 2378 | |
| 2379 | void classifyAttrOnRoot(Record *Attr) { |
| 2380 | bool result = classifyAttr(Attr); |
| 2381 | assert(result && "failed to classify on root"); (void) result; |
| 2382 | } |
| 2383 | |
| 2384 | void emitAttrRange(raw_ostream &OS) const { |
| 2385 | OS << "ATTR_RANGE(" << Descriptor.TableGenName |
| 2386 | << ", " << getFirstAttr()->getName() |
| 2387 | << ", " << getLastAttr()->getName() << ")\n"; |
| 2388 | } |
| 2389 | |
| 2390 | private: |
| 2391 | bool classifyAttr(Record *Attr) { |
| 2392 | // Check all the subclasses. |
| 2393 | for (auto SubClass : SubClasses) { |
| 2394 | if (SubClass->classifyAttr(Attr)) |
| 2395 | return true; |
| 2396 | } |
| 2397 | |
| 2398 | // It's not more specific than this class, but it might still belong here. |
| 2399 | if (Attr->isSubClassOf(TheRecord)) { |
| 2400 | Attrs.push_back(Attr); |
| 2401 | return true; |
| 2402 | } |
| 2403 | |
| 2404 | return false; |
| 2405 | } |
| 2406 | |
| 2407 | Record *getFirstAttr() const { |
| 2408 | if (!SubClasses.empty()) |
| 2409 | return SubClasses.front()->getFirstAttr(); |
| 2410 | return Attrs.front(); |
| 2411 | } |
| 2412 | |
| 2413 | Record *getLastAttr() const { |
| 2414 | if (!Attrs.empty()) |
| 2415 | return Attrs.back(); |
| 2416 | return SubClasses.back()->getLastAttr(); |
| 2417 | } |
| 2418 | }; |
| 2419 | |
| 2420 | /// The entire hierarchy of attribute classes. |
| 2421 | class AttrClassHierarchy { |
| 2422 | std::vector<std::unique_ptr<AttrClass>> Classes; |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 2423 | |
John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2424 | public: |
| 2425 | AttrClassHierarchy(RecordKeeper &Records) { |
| 2426 | // Find records for all the classes. |
| 2427 | for (auto &Descriptor : AttrClassDescriptors) { |
| 2428 | Record *ClassRecord = Records.getClass(Descriptor.TableGenName); |
| 2429 | AttrClass *Class = new AttrClass(Descriptor, ClassRecord); |
| 2430 | Classes.emplace_back(Class); |
| 2431 | } |
| 2432 | |
| 2433 | // Link up the hierarchy. |
| 2434 | for (auto &Class : Classes) { |
| 2435 | if (AttrClass *SuperClass = findSuperClass(Class->TheRecord)) { |
| 2436 | Class->SuperClass = SuperClass; |
| 2437 | SuperClass->SubClasses.push_back(Class.get()); |
| 2438 | } |
| 2439 | } |
| 2440 | |
| 2441 | #ifndef NDEBUG |
| 2442 | for (auto i = Classes.begin(), e = Classes.end(); i != e; ++i) { |
| 2443 | assert((i == Classes.begin()) == ((*i)->SuperClass == nullptr) && |
| 2444 | "only the first class should be a root class!"); |
| 2445 | } |
| 2446 | #endif |
| 2447 | } |
| 2448 | |
| 2449 | void emitDefaultDefines(raw_ostream &OS) const { |
| 2450 | for (auto &Class : Classes) { |
| 2451 | Class->emitDefaultDefines(OS); |
| 2452 | } |
| 2453 | } |
| 2454 | |
| 2455 | void emitUndefs(raw_ostream &OS) const { |
| 2456 | for (auto &Class : Classes) { |
| 2457 | Class->emitUndefs(OS); |
| 2458 | } |
| 2459 | } |
| 2460 | |
| 2461 | void emitAttrLists(raw_ostream &OS) const { |
| 2462 | // Just start from the root class. |
| 2463 | Classes[0]->emitAttrList(OS); |
| 2464 | } |
| 2465 | |
| 2466 | void emitAttrRanges(raw_ostream &OS) const { |
| 2467 | for (auto &Class : Classes) |
| 2468 | Class->emitAttrRange(OS); |
| 2469 | } |
| 2470 | |
| 2471 | void classifyAttr(Record *Attr) { |
| 2472 | // Add the attribute to the root class. |
| 2473 | Classes[0]->classifyAttrOnRoot(Attr); |
| 2474 | } |
| 2475 | |
| 2476 | private: |
| 2477 | AttrClass *findClassByRecord(Record *R) const { |
| 2478 | for (auto &Class : Classes) { |
| 2479 | if (Class->TheRecord == R) |
| 2480 | return Class.get(); |
| 2481 | } |
| 2482 | return nullptr; |
| 2483 | } |
| 2484 | |
| 2485 | AttrClass *findSuperClass(Record *R) const { |
| 2486 | // TableGen flattens the superclass list, so we just need to walk it |
| 2487 | // in reverse. |
| 2488 | auto SuperClasses = R->getSuperClasses(); |
| 2489 | for (signed i = 0, e = SuperClasses.size(); i != e; ++i) { |
| 2490 | auto SuperClass = findClassByRecord(SuperClasses[e - i - 1].first); |
| 2491 | if (SuperClass) return SuperClass; |
| 2492 | } |
| 2493 | return nullptr; |
| 2494 | } |
| 2495 | }; |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 2496 | |
| 2497 | } // end anonymous namespace |
John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2498 | |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 2499 | namespace clang { |
Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 2500 | |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 2501 | // Emits the enumeration list for attributes. |
| 2502 | void EmitClangAttrList(RecordKeeper &Records, raw_ostream &OS) { |
Dmitri Gribenko | 6b11fca | 2013-01-30 21:54:20 +0000 | [diff] [blame] | 2503 | emitSourceFileHeader("List of all attributes that Clang recognizes", OS); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2504 | |
John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2505 | AttrClassHierarchy Hierarchy(Records); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2506 | |
John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2507 | // Add defaulting macro definitions. |
| 2508 | Hierarchy.emitDefaultDefines(OS); |
| 2509 | emitDefaultDefine(OS, "PRAGMA_SPELLING_ATTR", nullptr); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2510 | |
John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2511 | std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); |
| 2512 | std::vector<Record *> PragmaAttrs; |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2513 | for (auto *Attr : Attrs) { |
| 2514 | if (!Attr->getValueAsBit("ASTNode")) |
Douglas Gregor | b2daf84 | 2012-05-02 15:56:52 +0000 | [diff] [blame] | 2515 | continue; |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 2516 | |
John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2517 | // Add the attribute to the ad-hoc groups. |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 2518 | if (AttrHasPragmaSpelling(Attr)) |
| 2519 | PragmaAttrs.push_back(Attr); |
| 2520 | |
John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2521 | // Place it in the hierarchy. |
| 2522 | Hierarchy.classifyAttr(Attr); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2525 | // Emit the main attribute list. |
| 2526 | Hierarchy.emitAttrLists(OS); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2527 | |
John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2528 | // Emit the ad hoc groups. |
| 2529 | emitAttrList(OS, "PRAGMA_SPELLING_ATTR", PragmaAttrs); |
| 2530 | |
| 2531 | // Emit the attribute ranges. |
| 2532 | OS << "#ifdef ATTR_RANGE\n"; |
| 2533 | Hierarchy.emitAttrRanges(OS); |
| 2534 | OS << "#undef ATTR_RANGE\n"; |
| 2535 | OS << "#endif\n"; |
| 2536 | |
| 2537 | Hierarchy.emitUndefs(OS); |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 2538 | OS << "#undef PRAGMA_SPELLING_ATTR\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 2541 | // Emits the enumeration list for attributes. |
| 2542 | void EmitClangAttrSubjectMatchRuleList(RecordKeeper &Records, raw_ostream &OS) { |
| 2543 | emitSourceFileHeader( |
| 2544 | "List of all attribute subject matching rules that Clang recognizes", OS); |
| 2545 | PragmaClangAttributeSupport &PragmaAttributeSupport = |
| 2546 | getPragmaAttributeSupport(Records); |
| 2547 | emitDefaultDefine(OS, "ATTR_MATCH_RULE", nullptr); |
| 2548 | PragmaAttributeSupport.emitMatchRuleList(OS); |
| 2549 | OS << "#undef ATTR_MATCH_RULE\n"; |
| 2550 | } |
| 2551 | |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 2552 | // Emits the code to read an attribute from a precompiled header. |
| 2553 | void EmitClangAttrPCHRead(RecordKeeper &Records, raw_ostream &OS) { |
Dmitri Gribenko | 6b11fca | 2013-01-30 21:54:20 +0000 | [diff] [blame] | 2554 | emitSourceFileHeader("Attribute deserialization code", OS); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2555 | |
| 2556 | Record *InhClass = Records.getClass("InheritableAttr"); |
| 2557 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), |
| 2558 | ArgRecords; |
Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2559 | std::vector<std::unique_ptr<Argument>> Args; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2560 | |
| 2561 | OS << " switch (Kind) {\n"; |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2562 | for (const auto *Attr : Attrs) { |
| 2563 | const Record &R = *Attr; |
Douglas Gregor | b2daf84 | 2012-05-02 15:56:52 +0000 | [diff] [blame] | 2564 | if (!R.getValueAsBit("ASTNode")) |
| 2565 | continue; |
| 2566 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2567 | OS << " case attr::" << R.getName() << ": {\n"; |
| 2568 | if (R.isSubClassOf(InhClass)) |
David L. Jones | 267b884 | 2017-01-24 01:04:30 +0000 | [diff] [blame] | 2569 | OS << " bool isInherited = Record.readInt();\n"; |
| 2570 | OS << " bool isImplicit = Record.readInt();\n"; |
| 2571 | OS << " unsigned Spelling = Record.readInt();\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2572 | ArgRecords = R.getValueAsListOfDefs("Args"); |
| 2573 | Args.clear(); |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2574 | for (const auto *Arg : ArgRecords) { |
| 2575 | Args.emplace_back(createArgument(*Arg, R.getName())); |
Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2576 | Args.back()->writePCHReadDecls(OS); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2577 | } |
| 2578 | OS << " New = new (Context) " << R.getName() << "Attr(Range, Context"; |
Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2579 | for (auto const &ri : Args) { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2580 | OS << ", "; |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 2581 | ri->writePCHReadArgs(OS); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2582 | } |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2583 | OS << ", Spelling);\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2584 | if (R.isSubClassOf(InhClass)) |
| 2585 | OS << " cast<InheritableAttr>(New)->setInherited(isInherited);\n"; |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2586 | OS << " New->setImplicit(isImplicit);\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2587 | OS << " break;\n"; |
| 2588 | OS << " }\n"; |
| 2589 | } |
| 2590 | OS << " }\n"; |
| 2591 | } |
| 2592 | |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 2593 | // Emits the code to write an attribute to a precompiled header. |
| 2594 | void EmitClangAttrPCHWrite(RecordKeeper &Records, raw_ostream &OS) { |
Dmitri Gribenko | 6b11fca | 2013-01-30 21:54:20 +0000 | [diff] [blame] | 2595 | emitSourceFileHeader("Attribute serialization code", OS); |
| 2596 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2597 | Record *InhClass = Records.getClass("InheritableAttr"); |
| 2598 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2599 | |
| 2600 | OS << " switch (A->getKind()) {\n"; |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2601 | for (const auto *Attr : Attrs) { |
| 2602 | const Record &R = *Attr; |
Douglas Gregor | b2daf84 | 2012-05-02 15:56:52 +0000 | [diff] [blame] | 2603 | if (!R.getValueAsBit("ASTNode")) |
| 2604 | continue; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2605 | OS << " case attr::" << R.getName() << ": {\n"; |
| 2606 | Args = R.getValueAsListOfDefs("Args"); |
| 2607 | if (R.isSubClassOf(InhClass) || !Args.empty()) |
Eugene Zelenko | 5f02b77 | 2015-12-08 18:49:01 +0000 | [diff] [blame] | 2608 | OS << " const auto *SA = cast<" << R.getName() |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2609 | << "Attr>(A);\n"; |
| 2610 | if (R.isSubClassOf(InhClass)) |
| 2611 | OS << " Record.push_back(SA->isInherited());\n"; |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2612 | OS << " Record.push_back(A->isImplicit());\n"; |
| 2613 | OS << " Record.push_back(A->getSpellingListIndex());\n"; |
| 2614 | |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2615 | for (const auto *Arg : Args) |
| 2616 | createArgument(*Arg, R.getName())->writePCHWrite(OS); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2617 | OS << " break;\n"; |
| 2618 | OS << " }\n"; |
| 2619 | } |
| 2620 | OS << " }\n"; |
| 2621 | } |
| 2622 | |
Bob Wilson | 0058b82 | 2015-07-20 22:57:36 +0000 | [diff] [blame] | 2623 | // Generate a conditional expression to check if the current target satisfies |
| 2624 | // the conditions for a TargetSpecificAttr record, and append the code for |
| 2625 | // those checks to the Test string. If the FnName string pointer is non-null, |
| 2626 | // append a unique suffix to distinguish this set of target checks from other |
| 2627 | // TargetSpecificAttr records. |
| 2628 | static void GenerateTargetSpecificAttrChecks(const Record *R, |
Craig Topper | 0064858 | 2017-05-31 19:01:22 +0000 | [diff] [blame] | 2629 | std::vector<StringRef> &Arches, |
Bob Wilson | 0058b82 | 2015-07-20 22:57:36 +0000 | [diff] [blame] | 2630 | std::string &Test, |
| 2631 | std::string *FnName) { |
| 2632 | // It is assumed that there will be an llvm::Triple object |
| 2633 | // named "T" and a TargetInfo object named "Target" within |
| 2634 | // scope that can be used to determine whether the attribute exists in |
| 2635 | // a given target. |
| 2636 | Test += "("; |
| 2637 | |
| 2638 | for (auto I = Arches.begin(), E = Arches.end(); I != E; ++I) { |
Craig Topper | 0064858 | 2017-05-31 19:01:22 +0000 | [diff] [blame] | 2639 | StringRef Part = *I; |
| 2640 | Test += "T.getArch() == llvm::Triple::"; |
| 2641 | Test += Part; |
Bob Wilson | 0058b82 | 2015-07-20 22:57:36 +0000 | [diff] [blame] | 2642 | if (I + 1 != E) |
| 2643 | Test += " || "; |
| 2644 | if (FnName) |
| 2645 | *FnName += Part; |
| 2646 | } |
| 2647 | Test += ")"; |
| 2648 | |
| 2649 | // If the attribute is specific to particular OSes, check those. |
| 2650 | if (!R->isValueUnset("OSes")) { |
| 2651 | // We know that there was at least one arch test, so we need to and in the |
| 2652 | // OS tests. |
| 2653 | Test += " && ("; |
Craig Topper | 0064858 | 2017-05-31 19:01:22 +0000 | [diff] [blame] | 2654 | std::vector<StringRef> OSes = R->getValueAsListOfStrings("OSes"); |
Bob Wilson | 0058b82 | 2015-07-20 22:57:36 +0000 | [diff] [blame] | 2655 | for (auto I = OSes.begin(), E = OSes.end(); I != E; ++I) { |
Craig Topper | 0064858 | 2017-05-31 19:01:22 +0000 | [diff] [blame] | 2656 | StringRef Part = *I; |
Bob Wilson | 0058b82 | 2015-07-20 22:57:36 +0000 | [diff] [blame] | 2657 | |
Craig Topper | 0064858 | 2017-05-31 19:01:22 +0000 | [diff] [blame] | 2658 | Test += "T.getOS() == llvm::Triple::"; |
| 2659 | Test += Part; |
Bob Wilson | 0058b82 | 2015-07-20 22:57:36 +0000 | [diff] [blame] | 2660 | if (I + 1 != E) |
| 2661 | Test += " || "; |
| 2662 | if (FnName) |
| 2663 | *FnName += Part; |
| 2664 | } |
| 2665 | Test += ")"; |
| 2666 | } |
| 2667 | |
| 2668 | // If one or more CXX ABIs are specified, check those as well. |
| 2669 | if (!R->isValueUnset("CXXABIs")) { |
| 2670 | Test += " && ("; |
Craig Topper | 0064858 | 2017-05-31 19:01:22 +0000 | [diff] [blame] | 2671 | std::vector<StringRef> CXXABIs = R->getValueAsListOfStrings("CXXABIs"); |
Bob Wilson | 0058b82 | 2015-07-20 22:57:36 +0000 | [diff] [blame] | 2672 | for (auto I = CXXABIs.begin(), E = CXXABIs.end(); I != E; ++I) { |
Craig Topper | 0064858 | 2017-05-31 19:01:22 +0000 | [diff] [blame] | 2673 | StringRef Part = *I; |
| 2674 | Test += "Target.getCXXABI().getKind() == TargetCXXABI::"; |
| 2675 | Test += Part; |
Bob Wilson | 0058b82 | 2015-07-20 22:57:36 +0000 | [diff] [blame] | 2676 | if (I + 1 != E) |
| 2677 | Test += " || "; |
| 2678 | if (FnName) |
| 2679 | *FnName += Part; |
| 2680 | } |
| 2681 | Test += ")"; |
| 2682 | } |
| 2683 | } |
| 2684 | |
Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2685 | static void GenerateHasAttrSpellingStringSwitch( |
| 2686 | const std::vector<Record *> &Attrs, raw_ostream &OS, |
| 2687 | const std::string &Variety = "", const std::string &Scope = "") { |
| 2688 | for (const auto *Attr : Attrs) { |
Aaron Ballman | a0344c5 | 2014-11-14 13:44:02 +0000 | [diff] [blame] | 2689 | // C++11-style attributes have specific version information associated with |
| 2690 | // them. If the attribute has no scope, the version information must not |
| 2691 | // have the default value (1), as that's incorrect. Instead, the unscoped |
| 2692 | // attribute version information should be taken from the SD-6 standing |
| 2693 | // document, which can be found at: |
| 2694 | // https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations |
| 2695 | int Version = 1; |
| 2696 | |
| 2697 | if (Variety == "CXX11") { |
| 2698 | std::vector<Record *> Spellings = Attr->getValueAsListOfDefs("Spellings"); |
| 2699 | for (const auto &Spelling : Spellings) { |
| 2700 | if (Spelling->getValueAsString("Variety") == "CXX11") { |
| 2701 | Version = static_cast<int>(Spelling->getValueAsInt("Version")); |
| 2702 | if (Scope.empty() && Version == 1) |
| 2703 | PrintError(Spelling->getLoc(), "C++ standard attributes must " |
| 2704 | "have valid version information."); |
| 2705 | break; |
| 2706 | } |
| 2707 | } |
| 2708 | } |
| 2709 | |
Aaron Ballman | 0fa06d8 | 2014-01-09 22:57:44 +0000 | [diff] [blame] | 2710 | std::string Test; |
Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2711 | if (Attr->isSubClassOf("TargetSpecificAttr")) { |
| 2712 | const Record *R = Attr->getValueAsDef("Target"); |
Craig Topper | 0064858 | 2017-05-31 19:01:22 +0000 | [diff] [blame] | 2713 | std::vector<StringRef> Arches = R->getValueAsListOfStrings("Arches"); |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 2714 | GenerateTargetSpecificAttrChecks(R, Arches, Test, nullptr); |
Bob Wilson | 7c73083 | 2015-07-20 22:57:31 +0000 | [diff] [blame] | 2715 | |
Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2716 | // If this is the C++11 variety, also add in the LangOpts test. |
| 2717 | if (Variety == "CXX11") |
| 2718 | Test += " && LangOpts.CPlusPlus11"; |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame^] | 2719 | else if (Variety == "C2x") |
| 2720 | Test += " && LangOpts.DoubleSquareBracketAttributes"; |
Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2721 | } else if (Variety == "CXX11") |
| 2722 | // C++11 mode should be checked against LangOpts, which is presumed to be |
| 2723 | // present in the caller. |
| 2724 | Test = "LangOpts.CPlusPlus11"; |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame^] | 2725 | else if (Variety == "C2x") |
| 2726 | Test = "LangOpts.DoubleSquareBracketAttributes"; |
Aaron Ballman | 0fa06d8 | 2014-01-09 22:57:44 +0000 | [diff] [blame] | 2727 | |
Aaron Ballman | a0344c5 | 2014-11-14 13:44:02 +0000 | [diff] [blame] | 2728 | std::string TestStr = |
Aaron Ballman | 28afa18 | 2014-11-17 18:17:19 +0000 | [diff] [blame] | 2729 | !Test.empty() ? Test + " ? " + llvm::itostr(Version) + " : 0" : "1"; |
Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2730 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr); |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 2731 | for (const auto &S : Spellings) |
Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2732 | if (Variety.empty() || (Variety == S.variety() && |
| 2733 | (Scope.empty() || Scope == S.nameSpace()))) |
Aaron Ballman | a0344c5 | 2014-11-14 13:44:02 +0000 | [diff] [blame] | 2734 | OS << " .Case(\"" << S.name() << "\", " << TestStr << ")\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2735 | } |
Aaron Ballman | a0344c5 | 2014-11-14 13:44:02 +0000 | [diff] [blame] | 2736 | OS << " .Default(0);\n"; |
Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
| 2739 | // Emits the list of spellings for attributes. |
| 2740 | void EmitClangAttrHasAttrImpl(RecordKeeper &Records, raw_ostream &OS) { |
| 2741 | emitSourceFileHeader("Code to implement the __has_attribute logic", OS); |
| 2742 | |
| 2743 | // Separate all of the attributes out into four group: generic, C++11, GNU, |
| 2744 | // and declspecs. Then generate a big switch statement for each of them. |
| 2745 | std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); |
Nico Weber | 20e0804 | 2016-09-03 02:55:10 +0000 | [diff] [blame] | 2746 | std::vector<Record *> Declspec, Microsoft, GNU, Pragma; |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame^] | 2747 | std::map<std::string, std::vector<Record *>> CXX, C2x; |
Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2748 | |
| 2749 | // Walk over the list of all attributes, and split them out based on the |
| 2750 | // spelling variety. |
| 2751 | for (auto *R : Attrs) { |
| 2752 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R); |
| 2753 | for (const auto &SI : Spellings) { |
Benjamin Kramer | 2e018ef | 2016-05-27 13:36:58 +0000 | [diff] [blame] | 2754 | const std::string &Variety = SI.variety(); |
Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2755 | if (Variety == "GNU") |
| 2756 | GNU.push_back(R); |
| 2757 | else if (Variety == "Declspec") |
| 2758 | Declspec.push_back(R); |
Nico Weber | 20e0804 | 2016-09-03 02:55:10 +0000 | [diff] [blame] | 2759 | else if (Variety == "Microsoft") |
| 2760 | Microsoft.push_back(R); |
Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 2761 | else if (Variety == "CXX11") |
Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2762 | CXX[SI.nameSpace()].push_back(R); |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame^] | 2763 | else if (Variety == "C2x") |
| 2764 | C2x[SI.nameSpace()].push_back(R); |
Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 2765 | else if (Variety == "Pragma") |
| 2766 | Pragma.push_back(R); |
Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2767 | } |
| 2768 | } |
| 2769 | |
Bob Wilson | 7c73083 | 2015-07-20 22:57:31 +0000 | [diff] [blame] | 2770 | OS << "const llvm::Triple &T = Target.getTriple();\n"; |
Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2771 | OS << "switch (Syntax) {\n"; |
Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2772 | OS << "case AttrSyntax::GNU:\n"; |
Aaron Ballman | a0344c5 | 2014-11-14 13:44:02 +0000 | [diff] [blame] | 2773 | OS << " return llvm::StringSwitch<int>(Name)\n"; |
Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2774 | GenerateHasAttrSpellingStringSwitch(GNU, OS, "GNU"); |
| 2775 | OS << "case AttrSyntax::Declspec:\n"; |
Aaron Ballman | a0344c5 | 2014-11-14 13:44:02 +0000 | [diff] [blame] | 2776 | OS << " return llvm::StringSwitch<int>(Name)\n"; |
Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2777 | GenerateHasAttrSpellingStringSwitch(Declspec, OS, "Declspec"); |
Nico Weber | 20e0804 | 2016-09-03 02:55:10 +0000 | [diff] [blame] | 2778 | OS << "case AttrSyntax::Microsoft:\n"; |
| 2779 | OS << " return llvm::StringSwitch<int>(Name)\n"; |
| 2780 | GenerateHasAttrSpellingStringSwitch(Microsoft, OS, "Microsoft"); |
Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 2781 | OS << "case AttrSyntax::Pragma:\n"; |
Aaron Ballman | a0344c5 | 2014-11-14 13:44:02 +0000 | [diff] [blame] | 2782 | OS << " return llvm::StringSwitch<int>(Name)\n"; |
Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 2783 | GenerateHasAttrSpellingStringSwitch(Pragma, OS, "Pragma"); |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame^] | 2784 | auto fn = [&OS](const char *Spelling, const char *Variety, |
| 2785 | const std::map<std::string, std::vector<Record *>> &List) { |
| 2786 | OS << "case AttrSyntax::" << Variety << ": {\n"; |
| 2787 | // C++11-style attributes are further split out based on the Scope. |
| 2788 | for (auto I = List.cbegin(), E = List.cend(); I != E; ++I) { |
| 2789 | if (I != List.cbegin()) |
| 2790 | OS << " else "; |
| 2791 | if (I->first.empty()) |
| 2792 | OS << "if (!Scope || Scope->getName() == \"\") {\n"; |
| 2793 | else |
| 2794 | OS << "if (Scope->getName() == \"" << I->first << "\") {\n"; |
| 2795 | OS << " return llvm::StringSwitch<int>(Name)\n"; |
| 2796 | GenerateHasAttrSpellingStringSwitch(I->second, OS, Spelling, I->first); |
| 2797 | OS << "}"; |
| 2798 | } |
| 2799 | OS << "\n}\n"; |
| 2800 | }; |
| 2801 | fn("CXX11", "CXX", CXX); |
| 2802 | fn("C2x", "C", C2x); |
Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2803 | OS << "}\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2804 | } |
| 2805 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2806 | void EmitClangAttrSpellingListIndex(RecordKeeper &Records, raw_ostream &OS) { |
Dmitri Gribenko | 6b11fca | 2013-01-30 21:54:20 +0000 | [diff] [blame] | 2807 | emitSourceFileHeader("Code to translate different attribute spellings " |
| 2808 | "into internal identifiers", OS); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2809 | |
John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2810 | OS << " switch (AttrKind) {\n"; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2811 | |
Aaron Ballman | 64e6986 | 2013-12-15 13:05:48 +0000 | [diff] [blame] | 2812 | ParsedAttrMap Attrs = getParsedAttrList(Records); |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 2813 | for (const auto &I : Attrs) { |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2814 | const Record &R = *I.second; |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 2815 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R); |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 2816 | OS << " case AT_" << I.first << ": {\n"; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 2817 | for (unsigned I = 0; I < Spellings.size(); ++ I) { |
Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 2818 | OS << " if (Name == \"" << Spellings[I].name() << "\" && " |
| 2819 | << "SyntaxUsed == " |
| 2820 | << StringSwitch<unsigned>(Spellings[I].variety()) |
| 2821 | .Case("GNU", 0) |
| 2822 | .Case("CXX11", 1) |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame^] | 2823 | .Case("C2x", 2) |
| 2824 | .Case("Declspec", 3) |
| 2825 | .Case("Microsoft", 4) |
| 2826 | .Case("Keyword", 5) |
| 2827 | .Case("Pragma", 6) |
Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 2828 | .Default(0) |
| 2829 | << " && Scope == \"" << Spellings[I].nameSpace() << "\")\n" |
| 2830 | << " return " << I << ";\n"; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2831 | } |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 2832 | |
| 2833 | OS << " break;\n"; |
| 2834 | OS << " }\n"; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2835 | } |
| 2836 | |
| 2837 | OS << " }\n"; |
Aaron Ballman | 64e6986 | 2013-12-15 13:05:48 +0000 | [diff] [blame] | 2838 | OS << " return 0;\n"; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2839 | } |
| 2840 | |
DeLesley Hutchins | c4a8243 | 2013-12-30 17:24:36 +0000 | [diff] [blame] | 2841 | // Emits code used by RecursiveASTVisitor to visit attributes |
| 2842 | void EmitClangAttrASTVisitor(RecordKeeper &Records, raw_ostream &OS) { |
| 2843 | emitSourceFileHeader("Used by RecursiveASTVisitor to visit attributes.", OS); |
| 2844 | |
| 2845 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"); |
| 2846 | |
| 2847 | // Write method declarations for Traverse* methods. |
| 2848 | // We emit this here because we only generate methods for attributes that |
| 2849 | // are declared as ASTNodes. |
| 2850 | OS << "#ifdef ATTR_VISITOR_DECLS_ONLY\n\n"; |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2851 | for (const auto *Attr : Attrs) { |
| 2852 | const Record &R = *Attr; |
DeLesley Hutchins | c4a8243 | 2013-12-30 17:24:36 +0000 | [diff] [blame] | 2853 | if (!R.getValueAsBit("ASTNode")) |
| 2854 | continue; |
| 2855 | OS << " bool Traverse" |
| 2856 | << R.getName() << "Attr(" << R.getName() << "Attr *A);\n"; |
| 2857 | OS << " bool Visit" |
| 2858 | << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n" |
| 2859 | << " return true; \n" |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 2860 | << " }\n"; |
DeLesley Hutchins | c4a8243 | 2013-12-30 17:24:36 +0000 | [diff] [blame] | 2861 | } |
| 2862 | OS << "\n#else // ATTR_VISITOR_DECLS_ONLY\n\n"; |
| 2863 | |
| 2864 | // Write individual Traverse* methods for each attribute class. |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2865 | for (const auto *Attr : Attrs) { |
| 2866 | const Record &R = *Attr; |
DeLesley Hutchins | c4a8243 | 2013-12-30 17:24:36 +0000 | [diff] [blame] | 2867 | if (!R.getValueAsBit("ASTNode")) |
| 2868 | continue; |
| 2869 | |
| 2870 | OS << "template <typename Derived>\n" |
DeLesley Hutchins | bb79c33 | 2013-12-30 21:03:02 +0000 | [diff] [blame] | 2871 | << "bool VISITORCLASS<Derived>::Traverse" |
DeLesley Hutchins | c4a8243 | 2013-12-30 17:24:36 +0000 | [diff] [blame] | 2872 | << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n" |
| 2873 | << " if (!getDerived().VisitAttr(A))\n" |
| 2874 | << " return false;\n" |
| 2875 | << " if (!getDerived().Visit" << R.getName() << "Attr(A))\n" |
| 2876 | << " return false;\n"; |
| 2877 | |
| 2878 | std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args"); |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2879 | for (const auto *Arg : ArgRecords) |
| 2880 | createArgument(*Arg, R.getName())->writeASTVisitorTraversal(OS); |
DeLesley Hutchins | c4a8243 | 2013-12-30 17:24:36 +0000 | [diff] [blame] | 2881 | |
| 2882 | OS << " return true;\n"; |
| 2883 | OS << "}\n\n"; |
| 2884 | } |
| 2885 | |
| 2886 | // Write generic Traverse routine |
| 2887 | OS << "template <typename Derived>\n" |
DeLesley Hutchins | bb79c33 | 2013-12-30 21:03:02 +0000 | [diff] [blame] | 2888 | << "bool VISITORCLASS<Derived>::TraverseAttr(Attr *A) {\n" |
DeLesley Hutchins | c4a8243 | 2013-12-30 17:24:36 +0000 | [diff] [blame] | 2889 | << " if (!A)\n" |
| 2890 | << " return true;\n" |
| 2891 | << "\n" |
John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2892 | << " switch (A->getKind()) {\n"; |
DeLesley Hutchins | c4a8243 | 2013-12-30 17:24:36 +0000 | [diff] [blame] | 2893 | |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2894 | for (const auto *Attr : Attrs) { |
| 2895 | const Record &R = *Attr; |
DeLesley Hutchins | c4a8243 | 2013-12-30 17:24:36 +0000 | [diff] [blame] | 2896 | if (!R.getValueAsBit("ASTNode")) |
| 2897 | continue; |
| 2898 | |
| 2899 | OS << " case attr::" << R.getName() << ":\n" |
| 2900 | << " return getDerived().Traverse" << R.getName() << "Attr(" |
| 2901 | << "cast<" << R.getName() << "Attr>(A));\n"; |
| 2902 | } |
John McCall | 5d7cf77 | 2016-03-01 02:09:20 +0000 | [diff] [blame] | 2903 | OS << " }\n"; // end switch |
| 2904 | OS << " llvm_unreachable(\"bad attribute kind\");\n"; |
DeLesley Hutchins | c4a8243 | 2013-12-30 17:24:36 +0000 | [diff] [blame] | 2905 | OS << "}\n"; // end function |
| 2906 | OS << "#endif // ATTR_VISITOR_DECLS_ONLY\n"; |
| 2907 | } |
| 2908 | |
Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 2909 | void EmitClangAttrTemplateInstantiateHelper(const std::vector<Record *> &Attrs, |
| 2910 | raw_ostream &OS, |
| 2911 | bool AppliesToDecl) { |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 2912 | |
Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 2913 | OS << " switch (At->getKind()) {\n"; |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2914 | for (const auto *Attr : Attrs) { |
| 2915 | const Record &R = *Attr; |
Douglas Gregor | b2daf84 | 2012-05-02 15:56:52 +0000 | [diff] [blame] | 2916 | if (!R.getValueAsBit("ASTNode")) |
| 2917 | continue; |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 2918 | OS << " case attr::" << R.getName() << ": {\n"; |
Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 2919 | bool ShouldClone = R.getValueAsBit("Clone") && |
| 2920 | (!AppliesToDecl || |
| 2921 | R.getValueAsBit("MeaningfulToClassTemplateDefinition")); |
Rafael Espindola | 7f90b7d | 2012-05-15 14:09:55 +0000 | [diff] [blame] | 2922 | |
| 2923 | if (!ShouldClone) { |
Hans Wennborg | 59dbe86 | 2015-09-29 20:56:43 +0000 | [diff] [blame] | 2924 | OS << " return nullptr;\n"; |
Rafael Espindola | 7f90b7d | 2012-05-15 14:09:55 +0000 | [diff] [blame] | 2925 | OS << " }\n"; |
| 2926 | continue; |
| 2927 | } |
| 2928 | |
Eugene Zelenko | 5f02b77 | 2015-12-08 18:49:01 +0000 | [diff] [blame] | 2929 | OS << " const auto *A = cast<" |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 2930 | << R.getName() << "Attr>(At);\n"; |
| 2931 | bool TDependent = R.getValueAsBit("TemplateDependent"); |
| 2932 | |
| 2933 | if (!TDependent) { |
| 2934 | OS << " return A->clone(C);\n"; |
| 2935 | OS << " }\n"; |
| 2936 | continue; |
| 2937 | } |
| 2938 | |
| 2939 | std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args"); |
Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2940 | std::vector<std::unique_ptr<Argument>> Args; |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 2941 | Args.reserve(ArgRecords.size()); |
| 2942 | |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2943 | for (const auto *ArgRecord : ArgRecords) |
Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2944 | Args.emplace_back(createArgument(*ArgRecord, R.getName())); |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 2945 | |
Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2946 | for (auto const &ai : Args) |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 2947 | ai->writeTemplateInstantiation(OS); |
Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2948 | |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 2949 | OS << " return new (C) " << R.getName() << "Attr(A->getLocation(), C"; |
Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2950 | for (auto const &ai : Args) { |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 2951 | OS << ", "; |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 2952 | ai->writeTemplateInstantiationArgs(OS); |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 2953 | } |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2954 | OS << ", A->getSpellingListIndex());\n }\n"; |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 2955 | } |
| 2956 | OS << " } // end switch\n" |
| 2957 | << " llvm_unreachable(\"Unknown attribute!\");\n" |
Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 2958 | << " return nullptr;\n"; |
| 2959 | } |
| 2960 | |
| 2961 | // Emits code to instantiate dependent attributes on templates. |
| 2962 | void EmitClangAttrTemplateInstantiate(RecordKeeper &Records, raw_ostream &OS) { |
| 2963 | emitSourceFileHeader("Template instantiation code for attributes", OS); |
| 2964 | |
| 2965 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"); |
| 2966 | |
| 2967 | OS << "namespace clang {\n" |
| 2968 | << "namespace sema {\n\n" |
| 2969 | << "Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, " |
| 2970 | << "Sema &S,\n" |
| 2971 | << " const MultiLevelTemplateArgumentList &TemplateArgs) {\n"; |
| 2972 | EmitClangAttrTemplateInstantiateHelper(Attrs, OS, /*AppliesToDecl*/false); |
| 2973 | OS << "}\n\n" |
| 2974 | << "Attr *instantiateTemplateAttributeForDecl(const Attr *At,\n" |
| 2975 | << " ASTContext &C, Sema &S,\n" |
| 2976 | << " const MultiLevelTemplateArgumentList &TemplateArgs) {\n"; |
| 2977 | EmitClangAttrTemplateInstantiateHelper(Attrs, OS, /*AppliesToDecl*/true); |
| 2978 | OS << "}\n\n" |
Benjamin Kramer | bf8da9d | 2012-02-06 11:13:08 +0000 | [diff] [blame] | 2979 | << "} // end namespace sema\n" |
| 2980 | << "} // end namespace clang\n"; |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 2981 | } |
| 2982 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 2983 | // Emits the list of parsed attributes. |
| 2984 | void EmitClangAttrParsedAttrList(RecordKeeper &Records, raw_ostream &OS) { |
| 2985 | emitSourceFileHeader("List of all attributes that Clang recognizes", OS); |
| 2986 | |
| 2987 | OS << "#ifndef PARSED_ATTR\n"; |
| 2988 | OS << "#define PARSED_ATTR(NAME) NAME\n"; |
| 2989 | OS << "#endif\n\n"; |
| 2990 | |
| 2991 | ParsedAttrMap Names = getParsedAttrList(Records); |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 2992 | for (const auto &I : Names) { |
| 2993 | OS << "PARSED_ATTR(" << I.first << ")\n"; |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 2994 | } |
| 2995 | } |
| 2996 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 2997 | static bool isArgVariadic(const Record &R, StringRef AttrName) { |
| 2998 | return createArgument(R, AttrName)->isVariadic(); |
| 2999 | } |
| 3000 | |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3001 | static void emitArgInfo(const Record &R, std::stringstream &OS) { |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3002 | // This function will count the number of arguments specified for the |
| 3003 | // attribute and emit the number of required arguments followed by the |
| 3004 | // number of optional arguments. |
| 3005 | std::vector<Record *> Args = R.getValueAsListOfDefs("Args"); |
| 3006 | unsigned ArgCount = 0, OptCount = 0; |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 3007 | bool HasVariadic = false; |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 3008 | for (const auto *Arg : Args) { |
George Burgess IV | 8a36ace | 2016-12-01 17:52:39 +0000 | [diff] [blame] | 3009 | // If the arg is fake, it's the user's job to supply it: general parsing |
| 3010 | // logic shouldn't need to know anything about it. |
| 3011 | if (Arg->getValueAsBit("Fake")) |
| 3012 | continue; |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3013 | Arg->getValueAsBit("Optional") ? ++OptCount : ++ArgCount; |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 3014 | if (!HasVariadic && isArgVariadic(*Arg, R.getName())) |
| 3015 | HasVariadic = true; |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3016 | } |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 3017 | |
| 3018 | // If there is a variadic argument, we will set the optional argument count |
| 3019 | // to its largest value. Since it's currently a 4-bit number, we set it to 15. |
| 3020 | OS << ArgCount << ", " << (HasVariadic ? 15 : OptCount); |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3021 | } |
| 3022 | |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3023 | static void GenerateDefaultAppertainsTo(raw_ostream &OS) { |
Aaron Ballman | 93b5cc6 | 2013-12-02 19:36:42 +0000 | [diff] [blame] | 3024 | OS << "static bool defaultAppertainsTo(Sema &, const AttributeList &,"; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3025 | OS << "const Decl *) {\n"; |
| 3026 | OS << " return true;\n"; |
| 3027 | OS << "}\n\n"; |
| 3028 | } |
| 3029 | |
| 3030 | static std::string CalculateDiagnostic(const Record &S) { |
| 3031 | // If the SubjectList object has a custom diagnostic associated with it, |
| 3032 | // return that directly. |
| 3033 | std::string CustomDiag = S.getValueAsString("CustomDiag"); |
| 3034 | if (!CustomDiag.empty()) |
| 3035 | return CustomDiag; |
| 3036 | |
| 3037 | // Given the list of subjects, determine what diagnostic best fits. |
| 3038 | enum { |
| 3039 | Func = 1U << 0, |
| 3040 | Var = 1U << 1, |
| 3041 | ObjCMethod = 1U << 2, |
| 3042 | Param = 1U << 3, |
| 3043 | Class = 1U << 4, |
Aaron Ballman | c1494bd | 2013-11-27 20:14:30 +0000 | [diff] [blame] | 3044 | GenericRecord = 1U << 5, |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3045 | Type = 1U << 6, |
| 3046 | ObjCIVar = 1U << 7, |
| 3047 | ObjCProp = 1U << 8, |
| 3048 | ObjCInterface = 1U << 9, |
| 3049 | Block = 1U << 10, |
| 3050 | Namespace = 1U << 11, |
Aaron Ballman | 981ba24 | 2014-05-20 14:10:53 +0000 | [diff] [blame] | 3051 | Field = 1U << 12, |
| 3052 | CXXMethod = 1U << 13, |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 3053 | ObjCProtocol = 1U << 14, |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 3054 | Enum = 1U << 15, |
| 3055 | Named = 1U << 16, |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3056 | }; |
| 3057 | uint32_t SubMask = 0; |
| 3058 | |
| 3059 | std::vector<Record *> Subjects = S.getValueAsListOfDefs("Subjects"); |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 3060 | for (const auto *Subject : Subjects) { |
| 3061 | const Record &R = *Subject; |
Aaron Ballman | 8046903 | 2013-11-29 14:57:58 +0000 | [diff] [blame] | 3062 | std::string Name; |
| 3063 | |
| 3064 | if (R.isSubClassOf("SubsetSubject")) { |
| 3065 | PrintError(R.getLoc(), "SubsetSubjects should use a custom diagnostic"); |
| 3066 | // As a fallback, look through the SubsetSubject to see what its base |
| 3067 | // type is, and use that. This needs to be updated if SubsetSubjects |
| 3068 | // are allowed within other SubsetSubjects. |
| 3069 | Name = R.getValueAsDef("Base")->getName(); |
| 3070 | } else |
| 3071 | Name = R.getName(); |
| 3072 | |
| 3073 | uint32_t V = StringSwitch<uint32_t>(Name) |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3074 | .Case("Function", Func) |
| 3075 | .Case("Var", Var) |
| 3076 | .Case("ObjCMethod", ObjCMethod) |
| 3077 | .Case("ParmVar", Param) |
| 3078 | .Case("TypedefName", Type) |
| 3079 | .Case("ObjCIvar", ObjCIVar) |
| 3080 | .Case("ObjCProperty", ObjCProp) |
Aaron Ballman | c1494bd | 2013-11-27 20:14:30 +0000 | [diff] [blame] | 3081 | .Case("Record", GenericRecord) |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3082 | .Case("ObjCInterface", ObjCInterface) |
Ted Kremenek | d980da2 | 2013-12-10 19:43:42 +0000 | [diff] [blame] | 3083 | .Case("ObjCProtocol", ObjCProtocol) |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3084 | .Case("Block", Block) |
| 3085 | .Case("CXXRecord", Class) |
| 3086 | .Case("Namespace", Namespace) |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3087 | .Case("Field", Field) |
| 3088 | .Case("CXXMethod", CXXMethod) |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 3089 | .Case("Enum", Enum) |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 3090 | .Case("Named", Named) |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3091 | .Default(0); |
| 3092 | if (!V) { |
| 3093 | // Something wasn't in our mapping, so be helpful and let the developer |
| 3094 | // know about it. |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3095 | PrintFatalError(R.getLoc(), "Unknown subject type: " + R.getName()); |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3096 | return ""; |
| 3097 | } |
| 3098 | |
| 3099 | SubMask |= V; |
| 3100 | } |
| 3101 | |
| 3102 | switch (SubMask) { |
| 3103 | // For the simple cases where there's only a single entry in the mask, we |
| 3104 | // don't have to resort to bit fiddling. |
| 3105 | case Func: return "ExpectedFunction"; |
| 3106 | case Var: return "ExpectedVariable"; |
| 3107 | case Param: return "ExpectedParameter"; |
| 3108 | case Class: return "ExpectedClass"; |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 3109 | case Enum: return "ExpectedEnum"; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3110 | case CXXMethod: |
| 3111 | // FIXME: Currently, this maps to ExpectedMethod based on existing code, |
| 3112 | // but should map to something a bit more accurate at some point. |
| 3113 | case ObjCMethod: return "ExpectedMethod"; |
| 3114 | case Type: return "ExpectedType"; |
| 3115 | case ObjCInterface: return "ExpectedObjectiveCInterface"; |
Ted Kremenek | d980da2 | 2013-12-10 19:43:42 +0000 | [diff] [blame] | 3116 | case ObjCProtocol: return "ExpectedObjectiveCProtocol"; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3117 | |
Aaron Ballman | c1494bd | 2013-11-27 20:14:30 +0000 | [diff] [blame] | 3118 | // "GenericRecord" means struct, union or class; check the language options |
| 3119 | // and if not compiling for C++, strip off the class part. Note that this |
| 3120 | // relies on the fact that the context for this declares "Sema &S". |
| 3121 | case GenericRecord: |
Aaron Ballman | 17046b8 | 2013-11-27 19:16:55 +0000 | [diff] [blame] | 3122 | return "(S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass : " |
| 3123 | "ExpectedStructOrUnion)"; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3124 | case Func | ObjCMethod | Block: return "ExpectedFunctionMethodOrBlock"; |
| 3125 | case Func | ObjCMethod | Class: return "ExpectedFunctionMethodOrClass"; |
| 3126 | case Func | Param: |
| 3127 | case Func | ObjCMethod | Param: return "ExpectedFunctionMethodOrParameter"; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3128 | case Func | ObjCMethod: return "ExpectedFunctionOrMethod"; |
| 3129 | case Func | Var: return "ExpectedVariableOrFunction"; |
Aaron Ballman | 604dfec | 2013-12-02 17:07:07 +0000 | [diff] [blame] | 3130 | |
| 3131 | // If not compiling for C++, the class portion does not apply. |
| 3132 | case Func | Var | Class: |
| 3133 | return "(S.getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass : " |
| 3134 | "ExpectedVariableOrFunction)"; |
| 3135 | |
Saleem Abdulrasool | 511f2e5 | 2016-07-15 20:41:10 +0000 | [diff] [blame] | 3136 | case Func | Var | Class | ObjCInterface: |
| 3137 | return "(S.getLangOpts().CPlusPlus" |
| 3138 | " ? ((S.getLangOpts().ObjC1 || S.getLangOpts().ObjC2)" |
| 3139 | " ? ExpectedFunctionVariableClassOrObjCInterface" |
| 3140 | " : ExpectedFunctionVariableOrClass)" |
| 3141 | " : ((S.getLangOpts().ObjC1 || S.getLangOpts().ObjC2)" |
| 3142 | " ? ExpectedFunctionVariableOrObjCInterface" |
| 3143 | " : ExpectedVariableOrFunction))"; |
| 3144 | |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3145 | case ObjCMethod | ObjCProp: return "ExpectedMethodOrProperty"; |
Argyrios Kyrtzidis | a7233bd | 2017-05-24 00:46:27 +0000 | [diff] [blame] | 3146 | case Func | ObjCMethod | ObjCProp: |
| 3147 | return "ExpectedFunctionOrMethodOrProperty"; |
Aaron Ballman | 173361e | 2014-07-16 20:28:10 +0000 | [diff] [blame] | 3148 | case ObjCProtocol | ObjCInterface: |
| 3149 | return "ExpectedObjectiveCInterfaceOrProtocol"; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3150 | case Field | Var: return "ExpectedFieldOrGlobalVar"; |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 3151 | |
| 3152 | case Named: |
| 3153 | return "ExpectedNamedDecl"; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3154 | } |
| 3155 | |
| 3156 | PrintFatalError(S.getLoc(), |
| 3157 | "Could not deduce diagnostic argument for Attr subjects"); |
| 3158 | |
| 3159 | return ""; |
| 3160 | } |
| 3161 | |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 3162 | static std::string GetSubjectWithSuffix(const Record *R) { |
George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 3163 | const std::string &B = R->getName(); |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 3164 | if (B == "DeclBase") |
| 3165 | return "Decl"; |
| 3166 | return B + "Decl"; |
| 3167 | } |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 3168 | |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3169 | static std::string functionNameForCustomAppertainsTo(const Record &Subject) { |
| 3170 | return "is" + Subject.getName().str(); |
| 3171 | } |
| 3172 | |
Aaron Ballman | 8046903 | 2013-11-29 14:57:58 +0000 | [diff] [blame] | 3173 | static std::string GenerateCustomAppertainsTo(const Record &Subject, |
| 3174 | raw_ostream &OS) { |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3175 | std::string FnName = functionNameForCustomAppertainsTo(Subject); |
Aaron Ballman | a358c90 | 2013-12-02 14:58:17 +0000 | [diff] [blame] | 3176 | |
Aaron Ballman | 8046903 | 2013-11-29 14:57:58 +0000 | [diff] [blame] | 3177 | // If this code has already been generated, simply return the previous |
| 3178 | // instance of it. |
| 3179 | static std::set<std::string> CustomSubjectSet; |
Eugene Zelenko | 5f02b77 | 2015-12-08 18:49:01 +0000 | [diff] [blame] | 3180 | auto I = CustomSubjectSet.find(FnName); |
Aaron Ballman | 8046903 | 2013-11-29 14:57:58 +0000 | [diff] [blame] | 3181 | if (I != CustomSubjectSet.end()) |
| 3182 | return *I; |
| 3183 | |
| 3184 | Record *Base = Subject.getValueAsDef("Base"); |
| 3185 | |
| 3186 | // Not currently support custom subjects within custom subjects. |
| 3187 | if (Base->isSubClassOf("SubsetSubject")) { |
| 3188 | PrintFatalError(Subject.getLoc(), |
| 3189 | "SubsetSubjects within SubsetSubjects is not supported"); |
| 3190 | return ""; |
| 3191 | } |
| 3192 | |
Aaron Ballman | 8046903 | 2013-11-29 14:57:58 +0000 | [diff] [blame] | 3193 | OS << "static bool " << FnName << "(const Decl *D) {\n"; |
Eugene Zelenko | 5f02b77 | 2015-12-08 18:49:01 +0000 | [diff] [blame] | 3194 | OS << " if (const auto *S = dyn_cast<"; |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 3195 | OS << GetSubjectWithSuffix(Base); |
Aaron Ballman | 4755304 | 2014-01-16 14:32:03 +0000 | [diff] [blame] | 3196 | OS << ">(D))\n"; |
| 3197 | OS << " return " << Subject.getValueAsString("CheckCode") << ";\n"; |
| 3198 | OS << " return false;\n"; |
Aaron Ballman | 8046903 | 2013-11-29 14:57:58 +0000 | [diff] [blame] | 3199 | OS << "}\n\n"; |
| 3200 | |
| 3201 | CustomSubjectSet.insert(FnName); |
| 3202 | return FnName; |
| 3203 | } |
| 3204 | |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3205 | static std::string GenerateAppertainsTo(const Record &Attr, raw_ostream &OS) { |
| 3206 | // If the attribute does not contain a Subjects definition, then use the |
| 3207 | // default appertainsTo logic. |
| 3208 | if (Attr.isValueUnset("Subjects")) |
Aaron Ballman | 93b5cc6 | 2013-12-02 19:36:42 +0000 | [diff] [blame] | 3209 | return "defaultAppertainsTo"; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3210 | |
| 3211 | const Record *SubjectObj = Attr.getValueAsDef("Subjects"); |
| 3212 | std::vector<Record*> Subjects = SubjectObj->getValueAsListOfDefs("Subjects"); |
| 3213 | |
| 3214 | // If the list of subjects is empty, it is assumed that the attribute |
| 3215 | // appertains to everything. |
| 3216 | if (Subjects.empty()) |
Aaron Ballman | 93b5cc6 | 2013-12-02 19:36:42 +0000 | [diff] [blame] | 3217 | return "defaultAppertainsTo"; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3218 | |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3219 | bool Warn = SubjectObj->getValueAsDef("Diag")->getValueAsBit("Warn"); |
| 3220 | |
| 3221 | // Otherwise, generate an appertainsTo check specific to this attribute which |
| 3222 | // checks all of the given subjects against the Decl passed in. Return the |
| 3223 | // name of that check to the caller. |
Matthias Braun | bbbf5d4 | 2016-12-04 05:55:09 +0000 | [diff] [blame] | 3224 | std::string FnName = "check" + Attr.getName().str() + "AppertainsTo"; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3225 | std::stringstream SS; |
| 3226 | SS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr, "; |
| 3227 | SS << "const Decl *D) {\n"; |
| 3228 | SS << " if ("; |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3229 | for (auto I = Subjects.begin(), E = Subjects.end(); I != E; ++I) { |
Aaron Ballman | 8046903 | 2013-11-29 14:57:58 +0000 | [diff] [blame] | 3230 | // If the subject has custom code associated with it, generate a function |
| 3231 | // for it. The function cannot be inlined into this check (yet) because it |
| 3232 | // requires the subject to be of a specific type, and were that information |
| 3233 | // inlined here, it would not support an attribute with multiple custom |
| 3234 | // subjects. |
| 3235 | if ((*I)->isSubClassOf("SubsetSubject")) { |
| 3236 | SS << "!" << GenerateCustomAppertainsTo(**I, OS) << "(D)"; |
| 3237 | } else { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 3238 | SS << "!isa<" << GetSubjectWithSuffix(*I) << ">(D)"; |
Aaron Ballman | 8046903 | 2013-11-29 14:57:58 +0000 | [diff] [blame] | 3239 | } |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3240 | |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3241 | if (I + 1 != E) |
| 3242 | SS << " && "; |
| 3243 | } |
| 3244 | SS << ") {\n"; |
| 3245 | SS << " S.Diag(Attr.getLoc(), diag::"; |
| 3246 | SS << (Warn ? "warn_attribute_wrong_decl_type" : |
| 3247 | "err_attribute_wrong_decl_type"); |
| 3248 | SS << ")\n"; |
| 3249 | SS << " << Attr.getName() << "; |
| 3250 | SS << CalculateDiagnostic(*SubjectObj) << ";\n"; |
| 3251 | SS << " return false;\n"; |
| 3252 | SS << " }\n"; |
| 3253 | SS << " return true;\n"; |
| 3254 | SS << "}\n\n"; |
| 3255 | |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3256 | OS << SS.str(); |
| 3257 | return FnName; |
| 3258 | } |
| 3259 | |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3260 | static void |
| 3261 | emitAttributeMatchRules(PragmaClangAttributeSupport &PragmaAttributeSupport, |
| 3262 | raw_ostream &OS) { |
| 3263 | OS << "static bool checkAttributeMatchRuleAppliesTo(const Decl *D, " |
| 3264 | << AttributeSubjectMatchRule::EnumName << " rule) {\n"; |
| 3265 | OS << " switch (rule) {\n"; |
| 3266 | for (const auto &Rule : PragmaAttributeSupport.Rules) { |
| 3267 | if (Rule.isAbstractRule()) { |
| 3268 | OS << " case " << Rule.getEnumValue() << ":\n"; |
| 3269 | OS << " assert(false && \"Abstract matcher rule isn't allowed\");\n"; |
| 3270 | OS << " return false;\n"; |
| 3271 | continue; |
| 3272 | } |
| 3273 | std::vector<Record *> Subjects = Rule.getSubjects(); |
| 3274 | assert(!Subjects.empty() && "Missing subjects"); |
| 3275 | OS << " case " << Rule.getEnumValue() << ":\n"; |
| 3276 | OS << " return "; |
| 3277 | for (auto I = Subjects.begin(), E = Subjects.end(); I != E; ++I) { |
| 3278 | // If the subject has custom code associated with it, use the function |
| 3279 | // that was generated for GenerateAppertainsTo to check if the declaration |
| 3280 | // is valid. |
| 3281 | if ((*I)->isSubClassOf("SubsetSubject")) |
| 3282 | OS << functionNameForCustomAppertainsTo(**I) << "(D)"; |
| 3283 | else |
| 3284 | OS << "isa<" << GetSubjectWithSuffix(*I) << ">(D)"; |
| 3285 | |
| 3286 | if (I + 1 != E) |
| 3287 | OS << " || "; |
| 3288 | } |
| 3289 | OS << ";\n"; |
| 3290 | } |
| 3291 | OS << " }\n"; |
| 3292 | OS << " llvm_unreachable(\"Invalid match rule\");\nreturn false;\n"; |
| 3293 | OS << "}\n\n"; |
| 3294 | } |
| 3295 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3296 | static void GenerateDefaultLangOptRequirements(raw_ostream &OS) { |
| 3297 | OS << "static bool defaultDiagnoseLangOpts(Sema &, "; |
| 3298 | OS << "const AttributeList &) {\n"; |
| 3299 | OS << " return true;\n"; |
| 3300 | OS << "}\n\n"; |
| 3301 | } |
| 3302 | |
| 3303 | static std::string GenerateLangOptRequirements(const Record &R, |
| 3304 | raw_ostream &OS) { |
| 3305 | // If the attribute has an empty or unset list of language requirements, |
| 3306 | // return the default handler. |
| 3307 | std::vector<Record *> LangOpts = R.getValueAsListOfDefs("LangOpts"); |
| 3308 | if (LangOpts.empty()) |
| 3309 | return "defaultDiagnoseLangOpts"; |
| 3310 | |
| 3311 | // Generate the test condition, as well as a unique function name for the |
| 3312 | // diagnostic test. The list of options should usually be short (one or two |
| 3313 | // options), and the uniqueness isn't strictly necessary (it is just for |
| 3314 | // codegen efficiency). |
| 3315 | std::string FnName = "check", Test; |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3316 | for (auto I = LangOpts.begin(), E = LangOpts.end(); I != E; ++I) { |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3317 | std::string Part = (*I)->getValueAsString("Name"); |
Eric Fiselier | 341e825 | 2016-09-02 18:53:31 +0000 | [diff] [blame] | 3318 | if ((*I)->getValueAsBit("Negated")) { |
| 3319 | FnName += "Not"; |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 3320 | Test += "!"; |
Eric Fiselier | 341e825 | 2016-09-02 18:53:31 +0000 | [diff] [blame] | 3321 | } |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3322 | Test += "S.LangOpts." + Part; |
| 3323 | if (I + 1 != E) |
| 3324 | Test += " || "; |
| 3325 | FnName += Part; |
| 3326 | } |
| 3327 | FnName += "LangOpts"; |
| 3328 | |
| 3329 | // If this code has already been generated, simply return the previous |
| 3330 | // instance of it. |
| 3331 | static std::set<std::string> CustomLangOptsSet; |
Eugene Zelenko | 5f02b77 | 2015-12-08 18:49:01 +0000 | [diff] [blame] | 3332 | auto I = CustomLangOptsSet.find(FnName); |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3333 | if (I != CustomLangOptsSet.end()) |
| 3334 | return *I; |
| 3335 | |
| 3336 | OS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr) {\n"; |
| 3337 | OS << " if (" << Test << ")\n"; |
| 3338 | OS << " return true;\n\n"; |
| 3339 | OS << " S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) "; |
| 3340 | OS << "<< Attr.getName();\n"; |
| 3341 | OS << " return false;\n"; |
| 3342 | OS << "}\n\n"; |
| 3343 | |
| 3344 | CustomLangOptsSet.insert(FnName); |
| 3345 | return FnName; |
| 3346 | } |
| 3347 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3348 | static void GenerateDefaultTargetRequirements(raw_ostream &OS) { |
Bob Wilson | 7c73083 | 2015-07-20 22:57:31 +0000 | [diff] [blame] | 3349 | OS << "static bool defaultTargetRequirements(const TargetInfo &) {\n"; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3350 | OS << " return true;\n"; |
| 3351 | OS << "}\n\n"; |
| 3352 | } |
| 3353 | |
| 3354 | static std::string GenerateTargetRequirements(const Record &Attr, |
| 3355 | const ParsedAttrMap &Dupes, |
| 3356 | raw_ostream &OS) { |
| 3357 | // If the attribute is not a target specific attribute, return the default |
| 3358 | // target handler. |
| 3359 | if (!Attr.isSubClassOf("TargetSpecificAttr")) |
| 3360 | return "defaultTargetRequirements"; |
| 3361 | |
| 3362 | // Get the list of architectures to be tested for. |
| 3363 | const Record *R = Attr.getValueAsDef("Target"); |
Craig Topper | 0064858 | 2017-05-31 19:01:22 +0000 | [diff] [blame] | 3364 | std::vector<StringRef> Arches = R->getValueAsListOfStrings("Arches"); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3365 | if (Arches.empty()) { |
| 3366 | PrintError(Attr.getLoc(), "Empty list of target architectures for a " |
| 3367 | "target-specific attr"); |
| 3368 | return "defaultTargetRequirements"; |
| 3369 | } |
| 3370 | |
| 3371 | // If there are other attributes which share the same parsed attribute kind, |
| 3372 | // such as target-specific attributes with a shared spelling, collapse the |
| 3373 | // duplicate architectures. This is required because a shared target-specific |
| 3374 | // attribute has only one AttributeList::Kind enumeration value, but it |
| 3375 | // applies to multiple target architectures. In order for the attribute to be |
| 3376 | // considered valid, all of its architectures need to be included. |
| 3377 | if (!Attr.isValueUnset("ParseKind")) { |
| 3378 | std::string APK = Attr.getValueAsString("ParseKind"); |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3379 | for (const auto &I : Dupes) { |
| 3380 | if (I.first == APK) { |
Craig Topper | 0064858 | 2017-05-31 19:01:22 +0000 | [diff] [blame] | 3381 | std::vector<StringRef> DA = |
| 3382 | I.second->getValueAsDef("Target")->getValueAsListOfStrings( |
| 3383 | "Arches"); |
| 3384 | Arches.insert(Arches.end(), DA.begin(), DA.end()); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3385 | } |
| 3386 | } |
| 3387 | } |
| 3388 | |
Bob Wilson | 0058b82 | 2015-07-20 22:57:36 +0000 | [diff] [blame] | 3389 | std::string FnName = "isTarget"; |
| 3390 | std::string Test; |
| 3391 | GenerateTargetSpecificAttrChecks(R, Arches, Test, &FnName); |
Bob Wilson | 7c73083 | 2015-07-20 22:57:31 +0000 | [diff] [blame] | 3392 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3393 | // If this code has already been generated, simply return the previous |
| 3394 | // instance of it. |
| 3395 | static std::set<std::string> CustomTargetSet; |
Eugene Zelenko | 5f02b77 | 2015-12-08 18:49:01 +0000 | [diff] [blame] | 3396 | auto I = CustomTargetSet.find(FnName); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3397 | if (I != CustomTargetSet.end()) |
| 3398 | return *I; |
| 3399 | |
Bob Wilson | 7c73083 | 2015-07-20 22:57:31 +0000 | [diff] [blame] | 3400 | OS << "static bool " << FnName << "(const TargetInfo &Target) {\n"; |
| 3401 | OS << " const llvm::Triple &T = Target.getTriple();\n"; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3402 | OS << " return " << Test << ";\n"; |
| 3403 | OS << "}\n\n"; |
| 3404 | |
| 3405 | CustomTargetSet.insert(FnName); |
| 3406 | return FnName; |
| 3407 | } |
| 3408 | |
Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 3409 | static void GenerateDefaultSpellingIndexToSemanticSpelling(raw_ostream &OS) { |
| 3410 | OS << "static unsigned defaultSpellingIndexToSemanticSpelling(" |
| 3411 | << "const AttributeList &Attr) {\n"; |
| 3412 | OS << " return UINT_MAX;\n"; |
| 3413 | OS << "}\n\n"; |
| 3414 | } |
| 3415 | |
| 3416 | static std::string GenerateSpellingIndexToSemanticSpelling(const Record &Attr, |
| 3417 | raw_ostream &OS) { |
| 3418 | // If the attribute does not have a semantic form, we can bail out early. |
| 3419 | if (!Attr.getValueAsBit("ASTNode")) |
| 3420 | return "defaultSpellingIndexToSemanticSpelling"; |
| 3421 | |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 3422 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr); |
Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 3423 | |
| 3424 | // If there are zero or one spellings, or all of the spellings share the same |
| 3425 | // name, we can also bail out early. |
| 3426 | if (Spellings.size() <= 1 || SpellingNamesAreCommon(Spellings)) |
| 3427 | return "defaultSpellingIndexToSemanticSpelling"; |
| 3428 | |
| 3429 | // Generate the enumeration we will use for the mapping. |
| 3430 | SemanticSpellingMap SemanticToSyntacticMap; |
| 3431 | std::string Enum = CreateSemanticSpellings(Spellings, SemanticToSyntacticMap); |
Matthias Braun | bbbf5d4 | 2016-12-04 05:55:09 +0000 | [diff] [blame] | 3432 | std::string Name = Attr.getName().str() + "AttrSpellingMap"; |
Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 3433 | |
| 3434 | OS << "static unsigned " << Name << "(const AttributeList &Attr) {\n"; |
| 3435 | OS << Enum; |
| 3436 | OS << " unsigned Idx = Attr.getAttributeSpellingListIndex();\n"; |
| 3437 | WriteSemanticSpellingSwitch("Idx", SemanticToSyntacticMap, OS); |
| 3438 | OS << "}\n\n"; |
| 3439 | |
| 3440 | return Name; |
| 3441 | } |
| 3442 | |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 3443 | static bool IsKnownToGCC(const Record &Attr) { |
| 3444 | // Look at the spellings for this subject; if there are any spellings which |
| 3445 | // claim to be known to GCC, the attribute is known to GCC. |
George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 3446 | return llvm::any_of( |
| 3447 | GetFlattenedSpellings(Attr), |
| 3448 | [](const FlattenedSpelling &S) { return S.knownToGCC(); }); |
Aaron Ballman | 9a99e0d | 2014-01-20 17:18:35 +0000 | [diff] [blame] | 3449 | } |
| 3450 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3451 | /// Emits the parsed attribute helpers |
| 3452 | void EmitClangAttrParsedAttrImpl(RecordKeeper &Records, raw_ostream &OS) { |
| 3453 | emitSourceFileHeader("Parsed attribute helpers", OS); |
| 3454 | |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3455 | PragmaClangAttributeSupport &PragmaAttributeSupport = |
| 3456 | getPragmaAttributeSupport(Records); |
| 3457 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3458 | // Get the list of parsed attributes, and accept the optional list of |
| 3459 | // duplicates due to the ParseKind. |
| 3460 | ParsedAttrMap Dupes; |
| 3461 | ParsedAttrMap Attrs = getParsedAttrList(Records, &Dupes); |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3462 | |
Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 3463 | // Generate the default appertainsTo, target and language option diagnostic, |
| 3464 | // and spelling list index mapping methods. |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3465 | GenerateDefaultAppertainsTo(OS); |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3466 | GenerateDefaultLangOptRequirements(OS); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3467 | GenerateDefaultTargetRequirements(OS); |
Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 3468 | GenerateDefaultSpellingIndexToSemanticSpelling(OS); |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3469 | |
| 3470 | // Generate the appertainsTo diagnostic methods and write their names into |
| 3471 | // another mapping. At the same time, generate the AttrInfoMap object |
| 3472 | // contents. Due to the reliance on generated code, use separate streams so |
| 3473 | // that code will not be interleaved. |
| 3474 | std::stringstream SS; |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3475 | for (auto I = Attrs.begin(), E = Attrs.end(); I != E; ++I) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3476 | // TODO: If the attribute's kind appears in the list of duplicates, that is |
| 3477 | // because it is a target-specific attribute that appears multiple times. |
| 3478 | // It would be beneficial to test whether the duplicates are "similar |
| 3479 | // enough" to each other to not cause problems. For instance, check that |
Alp Toker | 96cf758 | 2014-01-18 21:49:37 +0000 | [diff] [blame] | 3480 | // the spellings are identical, and custom parsing rules match, etc. |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3481 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3482 | // We need to generate struct instances based off ParsedAttrInfo from |
| 3483 | // AttributeList.cpp. |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3484 | SS << " { "; |
| 3485 | emitArgInfo(*I->second, SS); |
| 3486 | SS << ", " << I->second->getValueAsBit("HasCustomParsing"); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3487 | SS << ", " << I->second->isSubClassOf("TargetSpecificAttr"); |
| 3488 | SS << ", " << I->second->isSubClassOf("TypeAttr"); |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 3489 | SS << ", " << I->second->isSubClassOf("StmtAttr"); |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 3490 | SS << ", " << IsKnownToGCC(*I->second); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3491 | SS << ", " << PragmaAttributeSupport.isAttributedSupported(*I->second); |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3492 | SS << ", " << GenerateAppertainsTo(*I->second, OS); |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3493 | SS << ", " << GenerateLangOptRequirements(*I->second, OS); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3494 | SS << ", " << GenerateTargetRequirements(*I->second, Dupes, OS); |
Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 3495 | SS << ", " << GenerateSpellingIndexToSemanticSpelling(*I->second, OS); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3496 | SS << ", " |
| 3497 | << PragmaAttributeSupport.generateStrictConformsTo(*I->second, OS); |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3498 | SS << " }"; |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3499 | |
| 3500 | if (I + 1 != E) |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3501 | SS << ","; |
| 3502 | |
| 3503 | SS << " // AT_" << I->first << "\n"; |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3504 | } |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3505 | |
| 3506 | OS << "static const ParsedAttrInfo AttrInfoMap[AttributeList::UnknownAttribute + 1] = {\n"; |
| 3507 | OS << SS.str(); |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3508 | OS << "};\n\n"; |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3509 | |
| 3510 | // Generate the attribute match rules. |
| 3511 | emitAttributeMatchRules(PragmaAttributeSupport, OS); |
Michael Han | 4a04517 | 2012-03-07 00:12:16 +0000 | [diff] [blame] | 3512 | } |
| 3513 | |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 3514 | // Emits the kind list of parsed attributes |
| 3515 | void EmitClangAttrParsedAttrKinds(RecordKeeper &Records, raw_ostream &OS) { |
Dmitri Gribenko | 6b11fca | 2013-01-30 21:54:20 +0000 | [diff] [blame] | 3516 | emitSourceFileHeader("Attribute name matcher", OS); |
| 3517 | |
Aaron Ballman | 09e98ff | 2014-01-13 21:42:39 +0000 | [diff] [blame] | 3518 | std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); |
Nico Weber | 20e0804 | 2016-09-03 02:55:10 +0000 | [diff] [blame] | 3519 | std::vector<StringMatcher::StringPair> GNU, Declspec, Microsoft, CXX11, |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame^] | 3520 | Keywords, Pragma, C2x; |
Aaron Ballman | 64e6986 | 2013-12-15 13:05:48 +0000 | [diff] [blame] | 3521 | std::set<std::string> Seen; |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 3522 | for (const auto *A : Attrs) { |
| 3523 | const Record &Attr = *A; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 3524 | |
Michael Han | 4a04517 | 2012-03-07 00:12:16 +0000 | [diff] [blame] | 3525 | bool SemaHandler = Attr.getValueAsBit("SemaHandler"); |
Douglas Gregor | 19fbb8f | 2012-05-02 16:18:45 +0000 | [diff] [blame] | 3526 | bool Ignored = Attr.getValueAsBit("Ignored"); |
Douglas Gregor | 19fbb8f | 2012-05-02 16:18:45 +0000 | [diff] [blame] | 3527 | if (SemaHandler || Ignored) { |
Aaron Ballman | 09e98ff | 2014-01-13 21:42:39 +0000 | [diff] [blame] | 3528 | // Attribute spellings can be shared between target-specific attributes, |
| 3529 | // and can be shared between syntaxes for the same attribute. For |
| 3530 | // instance, an attribute can be spelled GNU<"interrupt"> for an ARM- |
| 3531 | // specific attribute, or MSP430-specific attribute. Additionally, an |
| 3532 | // attribute can be spelled GNU<"dllexport"> and Declspec<"dllexport"> |
| 3533 | // for the same semantic attribute. Ultimately, we need to map each of |
| 3534 | // these to a single AttributeList::Kind value, but the StringMatcher |
| 3535 | // class cannot handle duplicate match strings. So we generate a list of |
| 3536 | // string to match based on the syntax, and emit multiple string matchers |
| 3537 | // depending on the syntax used. |
Aaron Ballman | 64e6986 | 2013-12-15 13:05:48 +0000 | [diff] [blame] | 3538 | std::string AttrName; |
| 3539 | if (Attr.isSubClassOf("TargetSpecificAttr") && |
| 3540 | !Attr.isValueUnset("ParseKind")) { |
| 3541 | AttrName = Attr.getValueAsString("ParseKind"); |
| 3542 | if (Seen.find(AttrName) != Seen.end()) |
| 3543 | continue; |
| 3544 | Seen.insert(AttrName); |
| 3545 | } else |
| 3546 | AttrName = NormalizeAttrName(StringRef(Attr.getName())).str(); |
| 3547 | |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 3548 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr); |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3549 | for (const auto &S : Spellings) { |
Benjamin Kramer | 2e018ef | 2016-05-27 13:36:58 +0000 | [diff] [blame] | 3550 | const std::string &RawSpelling = S.name(); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 3551 | std::vector<StringMatcher::StringPair> *Matches = nullptr; |
Benjamin Kramer | 2e018ef | 2016-05-27 13:36:58 +0000 | [diff] [blame] | 3552 | std::string Spelling; |
| 3553 | const std::string &Variety = S.variety(); |
Aaron Ballman | 09e98ff | 2014-01-13 21:42:39 +0000 | [diff] [blame] | 3554 | if (Variety == "CXX11") { |
| 3555 | Matches = &CXX11; |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3556 | Spelling += S.nameSpace(); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3557 | Spelling += "::"; |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame^] | 3558 | } else if (Variety == "C2x") { |
| 3559 | Matches = &C2x; |
| 3560 | Spelling += S.nameSpace(); |
| 3561 | Spelling += "::"; |
Aaron Ballman | 09e98ff | 2014-01-13 21:42:39 +0000 | [diff] [blame] | 3562 | } else if (Variety == "GNU") |
| 3563 | Matches = &GNU; |
| 3564 | else if (Variety == "Declspec") |
| 3565 | Matches = &Declspec; |
Nico Weber | 20e0804 | 2016-09-03 02:55:10 +0000 | [diff] [blame] | 3566 | else if (Variety == "Microsoft") |
| 3567 | Matches = &Microsoft; |
Aaron Ballman | 09e98ff | 2014-01-13 21:42:39 +0000 | [diff] [blame] | 3568 | else if (Variety == "Keyword") |
| 3569 | Matches = &Keywords; |
Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 3570 | else if (Variety == "Pragma") |
| 3571 | Matches = &Pragma; |
Alexis Hunt | a0e54d4 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 3572 | |
Aaron Ballman | 09e98ff | 2014-01-13 21:42:39 +0000 | [diff] [blame] | 3573 | assert(Matches && "Unsupported spelling variety found"); |
| 3574 | |
Justin Lebar | 4086fe5 | 2017-01-05 16:51:54 +0000 | [diff] [blame] | 3575 | if (Variety == "GNU") |
| 3576 | Spelling += NormalizeGNUAttrSpelling(RawSpelling); |
| 3577 | else |
| 3578 | Spelling += RawSpelling; |
| 3579 | |
Douglas Gregor | 19fbb8f | 2012-05-02 16:18:45 +0000 | [diff] [blame] | 3580 | if (SemaHandler) |
Aaron Ballman | 09e98ff | 2014-01-13 21:42:39 +0000 | [diff] [blame] | 3581 | Matches->push_back(StringMatcher::StringPair(Spelling, |
| 3582 | "return AttributeList::AT_" + AttrName + ";")); |
Douglas Gregor | 19fbb8f | 2012-05-02 16:18:45 +0000 | [diff] [blame] | 3583 | else |
Aaron Ballman | 09e98ff | 2014-01-13 21:42:39 +0000 | [diff] [blame] | 3584 | Matches->push_back(StringMatcher::StringPair(Spelling, |
| 3585 | "return AttributeList::IgnoredAttribute;")); |
Michael Han | 4a04517 | 2012-03-07 00:12:16 +0000 | [diff] [blame] | 3586 | } |
| 3587 | } |
| 3588 | } |
Douglas Gregor | 377f99b | 2012-05-02 17:33:51 +0000 | [diff] [blame] | 3589 | |
Aaron Ballman | 09e98ff | 2014-01-13 21:42:39 +0000 | [diff] [blame] | 3590 | OS << "static AttributeList::Kind getAttrKind(StringRef Name, "; |
| 3591 | OS << "AttributeList::Syntax Syntax) {\n"; |
| 3592 | OS << " if (AttributeList::AS_GNU == Syntax) {\n"; |
| 3593 | StringMatcher("Name", GNU, OS).Emit(); |
| 3594 | OS << " } else if (AttributeList::AS_Declspec == Syntax) {\n"; |
| 3595 | StringMatcher("Name", Declspec, OS).Emit(); |
Nico Weber | 20e0804 | 2016-09-03 02:55:10 +0000 | [diff] [blame] | 3596 | OS << " } else if (AttributeList::AS_Microsoft == Syntax) {\n"; |
| 3597 | StringMatcher("Name", Microsoft, OS).Emit(); |
Aaron Ballman | 09e98ff | 2014-01-13 21:42:39 +0000 | [diff] [blame] | 3598 | OS << " } else if (AttributeList::AS_CXX11 == Syntax) {\n"; |
| 3599 | StringMatcher("Name", CXX11, OS).Emit(); |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame^] | 3600 | OS << " } else if (AttributeList::AS_C2x == Syntax) {\n"; |
| 3601 | StringMatcher("Name", C2x, OS).Emit(); |
Douglas Gregor | bec595a | 2015-06-19 18:27:45 +0000 | [diff] [blame] | 3602 | OS << " } else if (AttributeList::AS_Keyword == Syntax || "; |
| 3603 | OS << "AttributeList::AS_ContextSensitiveKeyword == Syntax) {\n"; |
Aaron Ballman | 09e98ff | 2014-01-13 21:42:39 +0000 | [diff] [blame] | 3604 | StringMatcher("Name", Keywords, OS).Emit(); |
Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 3605 | OS << " } else if (AttributeList::AS_Pragma == Syntax) {\n"; |
| 3606 | StringMatcher("Name", Pragma, OS).Emit(); |
Aaron Ballman | 09e98ff | 2014-01-13 21:42:39 +0000 | [diff] [blame] | 3607 | OS << " }\n"; |
| 3608 | OS << " return AttributeList::UnknownAttribute;\n" |
Douglas Gregor | 377f99b | 2012-05-02 17:33:51 +0000 | [diff] [blame] | 3609 | << "}\n"; |
Michael Han | 4a04517 | 2012-03-07 00:12:16 +0000 | [diff] [blame] | 3610 | } |
| 3611 | |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 3612 | // Emits the code to dump an attribute. |
| 3613 | void EmitClangAttrDump(RecordKeeper &Records, raw_ostream &OS) { |
Dmitri Gribenko | 6b11fca | 2013-01-30 21:54:20 +0000 | [diff] [blame] | 3614 | emitSourceFileHeader("Attribute dumper", OS); |
| 3615 | |
John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 3616 | OS << " switch (A->getKind()) {\n"; |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 3617 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args; |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 3618 | for (const auto *Attr : Attrs) { |
| 3619 | const Record &R = *Attr; |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 3620 | if (!R.getValueAsBit("ASTNode")) |
| 3621 | continue; |
| 3622 | OS << " case attr::" << R.getName() << ": {\n"; |
Aaron Ballman | bc90961 | 2014-01-22 21:51:20 +0000 | [diff] [blame] | 3623 | |
| 3624 | // If the attribute has a semantically-meaningful name (which is determined |
| 3625 | // by whether there is a Spelling enumeration for it), then write out the |
| 3626 | // spelling used for the attribute. |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 3627 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R); |
Aaron Ballman | bc90961 | 2014-01-22 21:51:20 +0000 | [diff] [blame] | 3628 | if (Spellings.size() > 1 && !SpellingNamesAreCommon(Spellings)) |
| 3629 | OS << " OS << \" \" << A->getSpelling();\n"; |
| 3630 | |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 3631 | Args = R.getValueAsListOfDefs("Args"); |
| 3632 | if (!Args.empty()) { |
Eugene Zelenko | 5f02b77 | 2015-12-08 18:49:01 +0000 | [diff] [blame] | 3633 | OS << " const auto *SA = cast<" << R.getName() |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 3634 | << "Attr>(A);\n"; |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 3635 | for (const auto *Arg : Args) |
| 3636 | createArgument(*Arg, R.getName())->writeDump(OS); |
Richard Trieu | de5cc7d | 2013-01-31 01:44:26 +0000 | [diff] [blame] | 3637 | |
Eugene Zelenko | 5f02b77 | 2015-12-08 18:49:01 +0000 | [diff] [blame] | 3638 | for (const auto *AI : Args) |
| 3639 | createArgument(*AI, R.getName())->writeDumpChildren(OS); |
Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 3640 | } |
| 3641 | OS << |
| 3642 | " break;\n" |
| 3643 | " }\n"; |
| 3644 | } |
| 3645 | OS << " }\n"; |
| 3646 | } |
| 3647 | |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 3648 | void EmitClangAttrParserStringSwitches(RecordKeeper &Records, |
| 3649 | raw_ostream &OS) { |
| 3650 | emitSourceFileHeader("Parser-related llvm::StringSwitch cases", OS); |
| 3651 | emitClangAttrArgContextList(Records, OS); |
| 3652 | emitClangAttrIdentifierArgList(Records, OS); |
| 3653 | emitClangAttrTypeArgList(Records, OS); |
| 3654 | emitClangAttrLateParsedList(Records, OS); |
| 3655 | } |
| 3656 | |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3657 | void EmitClangAttrSubjectMatchRulesParserStringSwitches(RecordKeeper &Records, |
| 3658 | raw_ostream &OS) { |
| 3659 | getPragmaAttributeSupport(Records).generateParsingHelpers(OS); |
| 3660 | } |
| 3661 | |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3662 | class DocumentationData { |
| 3663 | public: |
Aaron Ballman | 1a3e585 | 2014-02-17 16:18:32 +0000 | [diff] [blame] | 3664 | const Record *Documentation; |
| 3665 | const Record *Attribute; |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3666 | |
Aaron Ballman | 4de1b58 | 2014-02-19 22:59:32 +0000 | [diff] [blame] | 3667 | DocumentationData(const Record &Documentation, const Record &Attribute) |
| 3668 | : Documentation(&Documentation), Attribute(&Attribute) {} |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3669 | }; |
| 3670 | |
Aaron Ballman | 4de1b58 | 2014-02-19 22:59:32 +0000 | [diff] [blame] | 3671 | static void WriteCategoryHeader(const Record *DocCategory, |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3672 | raw_ostream &OS) { |
Aaron Ballman | 4de1b58 | 2014-02-19 22:59:32 +0000 | [diff] [blame] | 3673 | const std::string &Name = DocCategory->getValueAsString("Name"); |
| 3674 | OS << Name << "\n" << std::string(Name.length(), '=') << "\n"; |
| 3675 | |
| 3676 | // If there is content, print that as well. |
| 3677 | std::string ContentStr = DocCategory->getValueAsString("Content"); |
Benjamin Kramer | 5c40407 | 2015-04-10 21:37:21 +0000 | [diff] [blame] | 3678 | // Trim leading and trailing newlines and spaces. |
| 3679 | OS << StringRef(ContentStr).trim(); |
| 3680 | |
Aaron Ballman | 4de1b58 | 2014-02-19 22:59:32 +0000 | [diff] [blame] | 3681 | OS << "\n\n"; |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3682 | } |
| 3683 | |
Aaron Ballman | a66b574 | 2014-02-17 15:36:08 +0000 | [diff] [blame] | 3684 | enum SpellingKind { |
| 3685 | GNU = 1 << 0, |
| 3686 | CXX11 = 1 << 1, |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame^] | 3687 | C2x = 1 << 2, |
| 3688 | Declspec = 1 << 3, |
| 3689 | Microsoft = 1 << 4, |
| 3690 | Keyword = 1 << 5, |
| 3691 | Pragma = 1 << 6 |
Aaron Ballman | a66b574 | 2014-02-17 15:36:08 +0000 | [diff] [blame] | 3692 | }; |
| 3693 | |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3694 | static void WriteDocumentation(RecordKeeper &Records, |
| 3695 | const DocumentationData &Doc, raw_ostream &OS) { |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3696 | // FIXME: there is no way to have a per-spelling category for the attribute |
| 3697 | // documentation. This may not be a limiting factor since the spellings |
| 3698 | // should generally be consistently applied across the category. |
| 3699 | |
Aaron Ballman | 1a3e585 | 2014-02-17 16:18:32 +0000 | [diff] [blame] | 3700 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Doc.Attribute); |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3701 | |
| 3702 | // Determine the heading to be used for this attribute. |
Aaron Ballman | 1a3e585 | 2014-02-17 16:18:32 +0000 | [diff] [blame] | 3703 | std::string Heading = Doc.Documentation->getValueAsString("Heading"); |
Aaron Ballman | ea6668c | 2014-02-21 14:14:04 +0000 | [diff] [blame] | 3704 | bool CustomHeading = !Heading.empty(); |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3705 | if (Heading.empty()) { |
| 3706 | // If there's only one spelling, we can simply use that. |
| 3707 | if (Spellings.size() == 1) |
| 3708 | Heading = Spellings.begin()->name(); |
| 3709 | else { |
| 3710 | std::set<std::string> Uniques; |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3711 | for (auto I = Spellings.begin(), E = Spellings.end(); |
| 3712 | I != E && Uniques.size() <= 1; ++I) { |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3713 | std::string Spelling = NormalizeNameForSpellingComparison(I->name()); |
| 3714 | Uniques.insert(Spelling); |
| 3715 | } |
| 3716 | // If the semantic map has only one spelling, that is sufficient for our |
| 3717 | // needs. |
| 3718 | if (Uniques.size() == 1) |
| 3719 | Heading = *Uniques.begin(); |
| 3720 | } |
| 3721 | } |
| 3722 | |
| 3723 | // If the heading is still empty, it is an error. |
| 3724 | if (Heading.empty()) |
Aaron Ballman | 1a3e585 | 2014-02-17 16:18:32 +0000 | [diff] [blame] | 3725 | PrintFatalError(Doc.Attribute->getLoc(), |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3726 | "This attribute requires a heading to be specified"); |
| 3727 | |
| 3728 | // Gather a list of unique spellings; this is not the same as the semantic |
| 3729 | // spelling for the attribute. Variations in underscores and other non- |
| 3730 | // semantic characters are still acceptable. |
| 3731 | std::vector<std::string> Names; |
| 3732 | |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3733 | unsigned SupportedSpellings = 0; |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3734 | for (const auto &I : Spellings) { |
| 3735 | SpellingKind Kind = StringSwitch<SpellingKind>(I.variety()) |
Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 3736 | .Case("GNU", GNU) |
| 3737 | .Case("CXX11", CXX11) |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame^] | 3738 | .Case("C2x", C2x) |
Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 3739 | .Case("Declspec", Declspec) |
Nico Weber | 20e0804 | 2016-09-03 02:55:10 +0000 | [diff] [blame] | 3740 | .Case("Microsoft", Microsoft) |
Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 3741 | .Case("Keyword", Keyword) |
| 3742 | .Case("Pragma", Pragma); |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3743 | |
| 3744 | // Mask in the supported spelling. |
| 3745 | SupportedSpellings |= Kind; |
| 3746 | |
| 3747 | std::string Name; |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame^] | 3748 | if ((Kind == CXX11 || Kind == C2x) && !I.nameSpace().empty()) |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3749 | Name = I.nameSpace() + "::"; |
| 3750 | Name += I.name(); |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3751 | |
| 3752 | // If this name is the same as the heading, do not add it. |
| 3753 | if (Name != Heading) |
| 3754 | Names.push_back(Name); |
| 3755 | } |
| 3756 | |
| 3757 | // Print out the heading for the attribute. If there are alternate spellings, |
| 3758 | // then display those after the heading. |
Aaron Ballman | ea6668c | 2014-02-21 14:14:04 +0000 | [diff] [blame] | 3759 | if (!CustomHeading && !Names.empty()) { |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3760 | Heading += " ("; |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3761 | for (auto I = Names.begin(), E = Names.end(); I != E; ++I) { |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3762 | if (I != Names.begin()) |
| 3763 | Heading += ", "; |
| 3764 | Heading += *I; |
| 3765 | } |
| 3766 | Heading += ")"; |
| 3767 | } |
| 3768 | OS << Heading << "\n" << std::string(Heading.length(), '-') << "\n"; |
| 3769 | |
| 3770 | if (!SupportedSpellings) |
Aaron Ballman | 1a3e585 | 2014-02-17 16:18:32 +0000 | [diff] [blame] | 3771 | PrintFatalError(Doc.Attribute->getLoc(), |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3772 | "Attribute has no supported spellings; cannot be " |
| 3773 | "documented"); |
| 3774 | |
| 3775 | // List what spelling syntaxes the attribute supports. |
| 3776 | OS << ".. csv-table:: Supported Syntaxes\n"; |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame^] | 3777 | OS << " :header: \"GNU\", \"C++11\", \"C2x\", \"__declspec\", \"Keyword\","; |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3778 | OS << " \"Pragma\", \"Pragma clang attribute\"\n\n"; |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3779 | OS << " \""; |
| 3780 | if (SupportedSpellings & GNU) OS << "X"; |
| 3781 | OS << "\",\""; |
| 3782 | if (SupportedSpellings & CXX11) OS << "X"; |
| 3783 | OS << "\",\""; |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame^] | 3784 | if (SupportedSpellings & C2x) OS << "X"; |
| 3785 | OS << "\",\""; |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3786 | if (SupportedSpellings & Declspec) OS << "X"; |
| 3787 | OS << "\",\""; |
| 3788 | if (SupportedSpellings & Keyword) OS << "X"; |
Aaron Ballman | 120c79f | 2014-06-25 12:48:06 +0000 | [diff] [blame] | 3789 | OS << "\", \""; |
Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 3790 | if (SupportedSpellings & Pragma) OS << "X"; |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3791 | OS << "\", \""; |
| 3792 | if (getPragmaAttributeSupport(Records).isAttributedSupported(*Doc.Attribute)) |
| 3793 | OS << "X"; |
Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 3794 | OS << "\"\n\n"; |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3795 | |
| 3796 | // If the attribute is deprecated, print a message about it, and possibly |
| 3797 | // provide a replacement attribute. |
Aaron Ballman | 1a3e585 | 2014-02-17 16:18:32 +0000 | [diff] [blame] | 3798 | if (!Doc.Documentation->isValueUnset("Deprecated")) { |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3799 | OS << "This attribute has been deprecated, and may be removed in a future " |
| 3800 | << "version of Clang."; |
Aaron Ballman | 1a3e585 | 2014-02-17 16:18:32 +0000 | [diff] [blame] | 3801 | const Record &Deprecated = *Doc.Documentation->getValueAsDef("Deprecated"); |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3802 | std::string Replacement = Deprecated.getValueAsString("Replacement"); |
| 3803 | if (!Replacement.empty()) |
| 3804 | OS << " This attribute has been superseded by ``" |
| 3805 | << Replacement << "``."; |
| 3806 | OS << "\n\n"; |
| 3807 | } |
| 3808 | |
Aaron Ballman | 1a3e585 | 2014-02-17 16:18:32 +0000 | [diff] [blame] | 3809 | std::string ContentStr = Doc.Documentation->getValueAsString("Content"); |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3810 | // Trim leading and trailing newlines and spaces. |
Benjamin Kramer | 5c40407 | 2015-04-10 21:37:21 +0000 | [diff] [blame] | 3811 | OS << StringRef(ContentStr).trim(); |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3812 | |
| 3813 | OS << "\n\n\n"; |
| 3814 | } |
| 3815 | |
| 3816 | void EmitClangAttrDocs(RecordKeeper &Records, raw_ostream &OS) { |
| 3817 | // Get the documentation introduction paragraph. |
| 3818 | const Record *Documentation = Records.getDef("GlobalDocumentation"); |
| 3819 | if (!Documentation) { |
| 3820 | PrintFatalError("The Documentation top-level definition is missing, " |
| 3821 | "no documentation will be generated."); |
| 3822 | return; |
| 3823 | } |
| 3824 | |
Aaron Ballman | 4de1b58 | 2014-02-19 22:59:32 +0000 | [diff] [blame] | 3825 | OS << Documentation->getValueAsString("Intro") << "\n"; |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3826 | |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3827 | // Gather the Documentation lists from each of the attributes, based on the |
| 3828 | // category provided. |
| 3829 | std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3830 | std::map<const Record *, std::vector<DocumentationData>> SplitDocs; |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 3831 | for (const auto *A : Attrs) { |
| 3832 | const Record &Attr = *A; |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3833 | std::vector<Record *> Docs = Attr.getValueAsListOfDefs("Documentation"); |
Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 3834 | for (const auto *D : Docs) { |
| 3835 | const Record &Doc = *D; |
Aaron Ballman | 4de1b58 | 2014-02-19 22:59:32 +0000 | [diff] [blame] | 3836 | const Record *Category = Doc.getValueAsDef("Category"); |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3837 | // If the category is "undocumented", then there cannot be any other |
| 3838 | // documentation categories (otherwise, the attribute would become |
| 3839 | // documented). |
Aaron Ballman | 4de1b58 | 2014-02-19 22:59:32 +0000 | [diff] [blame] | 3840 | std::string Cat = Category->getValueAsString("Name"); |
| 3841 | bool Undocumented = Cat == "Undocumented"; |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3842 | if (Undocumented && Docs.size() > 1) |
| 3843 | PrintFatalError(Doc.getLoc(), |
| 3844 | "Attribute is \"Undocumented\", but has multiple " |
| 3845 | "documentation categories"); |
| 3846 | |
| 3847 | if (!Undocumented) |
Aaron Ballman | 4de1b58 | 2014-02-19 22:59:32 +0000 | [diff] [blame] | 3848 | SplitDocs[Category].push_back(DocumentationData(Doc, Attr)); |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3849 | } |
| 3850 | } |
| 3851 | |
| 3852 | // Having split the attributes out based on what documentation goes where, |
| 3853 | // we can begin to generate sections of documentation. |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3854 | for (const auto &I : SplitDocs) { |
| 3855 | WriteCategoryHeader(I.first, OS); |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3856 | |
| 3857 | // Walk over each of the attributes in the category and write out their |
| 3858 | // documentation. |
Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3859 | for (const auto &Doc : I.second) |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3860 | WriteDocumentation(Records, Doc, OS); |
| 3861 | } |
| 3862 | } |
| 3863 | |
| 3864 | void EmitTestPragmaAttributeSupportedAttributes(RecordKeeper &Records, |
| 3865 | raw_ostream &OS) { |
| 3866 | PragmaClangAttributeSupport Support = getPragmaAttributeSupport(Records); |
| 3867 | ParsedAttrMap Attrs = getParsedAttrList(Records); |
| 3868 | unsigned NumAttrs = 0; |
| 3869 | for (const auto &I : Attrs) { |
| 3870 | if (Support.isAttributedSupported(*I.second)) |
| 3871 | ++NumAttrs; |
| 3872 | } |
| 3873 | OS << "#pragma clang attribute supports " << NumAttrs << " attributes:\n"; |
| 3874 | for (const auto &I : Attrs) { |
| 3875 | if (!Support.isAttributedSupported(*I.second)) |
| 3876 | continue; |
| 3877 | OS << I.first; |
| 3878 | if (I.second->isValueUnset("Subjects")) { |
| 3879 | OS << " ()\n"; |
| 3880 | continue; |
| 3881 | } |
| 3882 | const Record *SubjectObj = I.second->getValueAsDef("Subjects"); |
| 3883 | std::vector<Record *> Subjects = |
| 3884 | SubjectObj->getValueAsListOfDefs("Subjects"); |
| 3885 | OS << " ("; |
| 3886 | for (const auto &Subject : llvm::enumerate(Subjects)) { |
| 3887 | if (Subject.index()) |
| 3888 | OS << ", "; |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 3889 | PragmaClangAttributeSupport::RuleOrAggregateRuleSet &RuleSet = |
| 3890 | Support.SubjectsToRules.find(Subject.value())->getSecond(); |
| 3891 | if (RuleSet.isRule()) { |
| 3892 | OS << RuleSet.getRule().getEnumValueName(); |
| 3893 | continue; |
| 3894 | } |
| 3895 | OS << "("; |
| 3896 | for (const auto &Rule : llvm::enumerate(RuleSet.getAggregateRuleSet())) { |
| 3897 | if (Rule.index()) |
| 3898 | OS << ", "; |
| 3899 | OS << Rule.value().getEnumValueName(); |
| 3900 | } |
| 3901 | OS << ")"; |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3902 | } |
| 3903 | OS << ")\n"; |
Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3904 | } |
| 3905 | } |
| 3906 | |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 3907 | } // end namespace clang |