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