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