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