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