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