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