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