| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1 | //===- ClangAttrEmitter.cpp - Generate Clang attribute handling =-*- C++ -*--=// | 
|  | 2 | // | 
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | 
|  | 4 | // See https://llvm.org/LICENSE.txt for license information. | 
|  | 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 6 | // | 
|  | 7 | //===----------------------------------------------------------------------===// | 
|  | 8 | // | 
|  | 9 | // These tablegen backends emit Clang attribute processing code | 
|  | 10 | // | 
|  | 11 | //===----------------------------------------------------------------------===// | 
|  | 12 |  | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/ArrayRef.h" | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/DenseMap.h" | 
| George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/DenseSet.h" | 
| Alex Lorenz | 3bfe962 | 2017-04-18 10:46:41 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallString.h" | 
| Aaron Ballman | 28afa18 | 2014-11-17 18:17:19 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringExtras.h" | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringRef.h" | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringSet.h" | 
| Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringSwitch.h" | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/iterator_range.h" | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" | 
|  | 24 | #include "llvm/Support/raw_ostream.h" | 
| Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 25 | #include "llvm/TableGen/Error.h" | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 26 | #include "llvm/TableGen/Record.h" | 
| Douglas Gregor | 377f99b | 2012-05-02 17:33:51 +0000 | [diff] [blame] | 27 | #include "llvm/TableGen/StringMatcher.h" | 
| Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 28 | #include "llvm/TableGen/TableGenBackend.h" | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 29 | #include <algorithm> | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 30 | #include <cassert> | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 31 | #include <cctype> | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 32 | #include <cstddef> | 
|  | 33 | #include <cstdint> | 
|  | 34 | #include <map> | 
| Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 35 | #include <memory> | 
| Aaron Ballman | 8046903 | 2013-11-29 14:57:58 +0000 | [diff] [blame] | 36 | #include <set> | 
| Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 37 | #include <sstream> | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 38 | #include <string> | 
|  | 39 | #include <utility> | 
|  | 40 | #include <vector> | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 41 |  | 
|  | 42 | using namespace llvm; | 
|  | 43 |  | 
| Benjamin Kramer | d910d16 | 2015-03-10 18:24:01 +0000 | [diff] [blame] | 44 | namespace { | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 45 |  | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 46 | class FlattenedSpelling { | 
|  | 47 | std::string V, N, NS; | 
|  | 48 | bool K; | 
|  | 49 |  | 
|  | 50 | public: | 
|  | 51 | FlattenedSpelling(const std::string &Variety, const std::string &Name, | 
|  | 52 | const std::string &Namespace, bool KnownToGCC) : | 
|  | 53 | V(Variety), N(Name), NS(Namespace), K(KnownToGCC) {} | 
|  | 54 | explicit FlattenedSpelling(const Record &Spelling) : | 
|  | 55 | V(Spelling.getValueAsString("Variety")), | 
|  | 56 | N(Spelling.getValueAsString("Name")) { | 
|  | 57 |  | 
| Aaron Ballman | ffc4336 | 2017-10-26 12:19:02 +0000 | [diff] [blame] | 58 | assert(V != "GCC" && V != "Clang" && | 
|  | 59 | "Given a GCC spelling, which means this hasn't been flattened!"); | 
| Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 60 | if (V == "CXX11" || V == "C2x" || V == "Pragma") | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 61 | NS = Spelling.getValueAsString("Namespace"); | 
|  | 62 | bool Unset; | 
|  | 63 | K = Spelling.getValueAsBitOrUnset("KnownToGCC", Unset); | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | const std::string &variety() const { return V; } | 
|  | 67 | const std::string &name() const { return N; } | 
|  | 68 | const std::string &nameSpace() const { return NS; } | 
|  | 69 | bool knownToGCC() const { return K; } | 
|  | 70 | }; | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 71 |  | 
| Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 72 | } // end anonymous namespace | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 73 |  | 
| Benjamin Kramer | d910d16 | 2015-03-10 18:24:01 +0000 | [diff] [blame] | 74 | static std::vector<FlattenedSpelling> | 
|  | 75 | GetFlattenedSpellings(const Record &Attr) { | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 76 | std::vector<Record *> Spellings = Attr.getValueAsListOfDefs("Spellings"); | 
|  | 77 | std::vector<FlattenedSpelling> Ret; | 
|  | 78 |  | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 79 | for (const auto &Spelling : Spellings) { | 
| Aaron Ballman | ffc4336 | 2017-10-26 12:19:02 +0000 | [diff] [blame] | 80 | StringRef Variety = Spelling->getValueAsString("Variety"); | 
|  | 81 | StringRef Name = Spelling->getValueAsString("Name"); | 
|  | 82 | if (Variety == "GCC") { | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 83 | // Gin up two new spelling objects to add into the list. | 
| Aaron Ballman | ffc4336 | 2017-10-26 12:19:02 +0000 | [diff] [blame] | 84 | Ret.emplace_back("GNU", Name, "", true); | 
|  | 85 | Ret.emplace_back("CXX11", Name, "gnu", true); | 
|  | 86 | } else if (Variety == "Clang") { | 
|  | 87 | Ret.emplace_back("GNU", Name, "", false); | 
|  | 88 | Ret.emplace_back("CXX11", Name, "clang", false); | 
| Aaron Ballman | 1000781 | 2018-01-03 22:22:48 +0000 | [diff] [blame] | 89 | if (Spelling->getValueAsBit("AllowInC")) | 
|  | 90 | Ret.emplace_back("C2x", Name, "clang", false); | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 91 | } else | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 92 | Ret.push_back(FlattenedSpelling(*Spelling)); | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 93 | } | 
|  | 94 |  | 
|  | 95 | return Ret; | 
|  | 96 | } | 
|  | 97 |  | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 98 | static std::string ReadPCHRecord(StringRef type) { | 
|  | 99 | return StringSwitch<std::string>(type) | 
| David L. Jones | 267b884 | 2017-01-24 01:04:30 +0000 | [diff] [blame] | 100 | .EndsWith("Decl *", "Record.GetLocalDeclAs<" | 
|  | 101 | + std::string(type, 0, type.size()-1) + ">(Record.readInt())") | 
|  | 102 | .Case("TypeSourceInfo *", "Record.getTypeSourceInfo()") | 
|  | 103 | .Case("Expr *", "Record.readExpr()") | 
|  | 104 | .Case("IdentifierInfo *", "Record.getIdentifierInfo()") | 
|  | 105 | .Case("StringRef", "Record.readString()") | 
| Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 106 | .Case("ParamIdx", "ParamIdx::deserialize(Record.readInt())") | 
| David L. Jones | 267b884 | 2017-01-24 01:04:30 +0000 | [diff] [blame] | 107 | .Default("Record.readInt()"); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 108 | } | 
|  | 109 |  | 
| Richard Smith | 1997856 | 2016-05-18 00:16:51 +0000 | [diff] [blame] | 110 | // Get a type that is suitable for storing an object of the specified type. | 
|  | 111 | static StringRef getStorageType(StringRef type) { | 
|  | 112 | return StringSwitch<StringRef>(type) | 
|  | 113 | .Case("StringRef", "std::string") | 
|  | 114 | .Default(type); | 
|  | 115 | } | 
|  | 116 |  | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 117 | // Assumes that the way to get the value is SA->getname() | 
|  | 118 | static std::string WritePCHRecord(StringRef type, StringRef name) { | 
| Richard Smith | 290d801 | 2016-04-06 17:06:00 +0000 | [diff] [blame] | 119 | return "Record." + StringSwitch<std::string>(type) | 
|  | 120 | .EndsWith("Decl *", "AddDeclRef(" + std::string(name) + ");\n") | 
|  | 121 | .Case("TypeSourceInfo *", "AddTypeSourceInfo(" + std::string(name) + ");\n") | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 122 | .Case("Expr *", "AddStmt(" + std::string(name) + ");\n") | 
| Richard Smith | 290d801 | 2016-04-06 17:06:00 +0000 | [diff] [blame] | 123 | .Case("IdentifierInfo *", "AddIdentifierRef(" + std::string(name) + ");\n") | 
|  | 124 | .Case("StringRef", "AddString(" + std::string(name) + ");\n") | 
| Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 125 | .Case("ParamIdx", "push_back(" + std::string(name) + ".serialize());\n") | 
| Richard Smith | 290d801 | 2016-04-06 17:06:00 +0000 | [diff] [blame] | 126 | .Default("push_back(" + std::string(name) + ");\n"); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 127 | } | 
|  | 128 |  | 
| Michael Han | 4a04517 | 2012-03-07 00:12:16 +0000 | [diff] [blame] | 129 | // Normalize attribute name by removing leading and trailing | 
|  | 130 | // underscores. For example, __foo, foo__, __foo__ would | 
|  | 131 | // become foo. | 
|  | 132 | static StringRef NormalizeAttrName(StringRef AttrName) { | 
| George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 133 | AttrName.consume_front("__"); | 
|  | 134 | AttrName.consume_back("__"); | 
| Michael Han | 4a04517 | 2012-03-07 00:12:16 +0000 | [diff] [blame] | 135 | return AttrName; | 
|  | 136 | } | 
|  | 137 |  | 
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 138 | // Normalize the name by removing any and all leading and trailing underscores. | 
|  | 139 | // This is different from NormalizeAttrName in that it also handles names like | 
|  | 140 | // _pascal and __pascal. | 
|  | 141 | static StringRef NormalizeNameForSpellingComparison(StringRef Name) { | 
| Benjamin Kramer | 5c40407 | 2015-04-10 21:37:21 +0000 | [diff] [blame] | 142 | return Name.trim("_"); | 
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 143 | } | 
|  | 144 |  | 
| Justin Lebar | 4086fe5 | 2017-01-05 16:51:54 +0000 | [diff] [blame] | 145 | // Normalize the spelling of a GNU attribute (i.e. "x" in "__attribute__((x))"), | 
|  | 146 | // removing "__" if it appears at the beginning and end of the attribute's name. | 
|  | 147 | static StringRef NormalizeGNUAttrSpelling(StringRef AttrSpelling) { | 
| Michael Han | 4a04517 | 2012-03-07 00:12:16 +0000 | [diff] [blame] | 148 | if (AttrSpelling.startswith("__") && AttrSpelling.endswith("__")) { | 
|  | 149 | AttrSpelling = AttrSpelling.substr(2, AttrSpelling.size() - 4); | 
|  | 150 | } | 
|  | 151 |  | 
|  | 152 | return AttrSpelling; | 
|  | 153 | } | 
|  | 154 |  | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 155 | typedef std::vector<std::pair<std::string, const Record *>> ParsedAttrMap; | 
| Aaron Ballman | 64e6986 | 2013-12-15 13:05:48 +0000 | [diff] [blame] | 156 |  | 
| Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 157 | static ParsedAttrMap getParsedAttrList(const RecordKeeper &Records, | 
| Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 158 | ParsedAttrMap *Dupes = nullptr) { | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 159 | std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); | 
| Aaron Ballman | 64e6986 | 2013-12-15 13:05:48 +0000 | [diff] [blame] | 160 | std::set<std::string> Seen; | 
|  | 161 | ParsedAttrMap R; | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 162 | for (const auto *Attr : Attrs) { | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 163 | if (Attr->getValueAsBit("SemaHandler")) { | 
| Aaron Ballman | 64e6986 | 2013-12-15 13:05:48 +0000 | [diff] [blame] | 164 | std::string AN; | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 165 | if (Attr->isSubClassOf("TargetSpecificAttr") && | 
|  | 166 | !Attr->isValueUnset("ParseKind")) { | 
|  | 167 | AN = Attr->getValueAsString("ParseKind"); | 
| Aaron Ballman | 64e6986 | 2013-12-15 13:05:48 +0000 | [diff] [blame] | 168 |  | 
|  | 169 | // If this attribute has already been handled, it does not need to be | 
|  | 170 | // handled again. | 
| Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 171 | if (Seen.find(AN) != Seen.end()) { | 
|  | 172 | if (Dupes) | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 173 | Dupes->push_back(std::make_pair(AN, Attr)); | 
| Aaron Ballman | 64e6986 | 2013-12-15 13:05:48 +0000 | [diff] [blame] | 174 | continue; | 
| Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 175 | } | 
| Aaron Ballman | 64e6986 | 2013-12-15 13:05:48 +0000 | [diff] [blame] | 176 | Seen.insert(AN); | 
|  | 177 | } else | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 178 | AN = NormalizeAttrName(Attr->getName()).str(); | 
| Aaron Ballman | 64e6986 | 2013-12-15 13:05:48 +0000 | [diff] [blame] | 179 |  | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 180 | R.push_back(std::make_pair(AN, Attr)); | 
| Aaron Ballman | 64e6986 | 2013-12-15 13:05:48 +0000 | [diff] [blame] | 181 | } | 
|  | 182 | } | 
|  | 183 | return R; | 
|  | 184 | } | 
|  | 185 |  | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 186 | namespace { | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 187 |  | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 188 | class Argument { | 
|  | 189 | std::string lowerName, upperName; | 
|  | 190 | StringRef attrName; | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 191 | bool isOpt; | 
| John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 192 | bool Fake; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 193 |  | 
|  | 194 | public: | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 195 | Argument(const Record &Arg, StringRef Attr) | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 196 | : lowerName(Arg.getValueAsString("Name")), upperName(lowerName), | 
| John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 197 | attrName(Attr), isOpt(false), Fake(false) { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 198 | if (!lowerName.empty()) { | 
|  | 199 | lowerName[0] = std::tolower(lowerName[0]); | 
|  | 200 | upperName[0] = std::toupper(upperName[0]); | 
|  | 201 | } | 
| Reid Kleckner | ebeb0ca | 2016-05-31 17:42:56 +0000 | [diff] [blame] | 202 | // Work around MinGW's macro definition of 'interface' to 'struct'. We | 
|  | 203 | // have an attribute argument called 'Interface', so only the lower case | 
|  | 204 | // name conflicts with the macro definition. | 
|  | 205 | if (lowerName == "interface") | 
|  | 206 | lowerName = "interface_"; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 207 | } | 
| Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 208 | virtual ~Argument() = default; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 209 |  | 
|  | 210 | StringRef getLowerName() const { return lowerName; } | 
|  | 211 | StringRef getUpperName() const { return upperName; } | 
|  | 212 | StringRef getAttrName() const { return attrName; } | 
|  | 213 |  | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 214 | bool isOptional() const { return isOpt; } | 
|  | 215 | void setOptional(bool set) { isOpt = set; } | 
|  | 216 |  | 
| John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 217 | bool isFake() const { return Fake; } | 
|  | 218 | void setFake(bool fake) { Fake = fake; } | 
|  | 219 |  | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 220 | // These functions print the argument contents formatted in different ways. | 
|  | 221 | virtual void writeAccessors(raw_ostream &OS) const = 0; | 
|  | 222 | virtual void writeAccessorDefinitions(raw_ostream &OS) const {} | 
| DeLesley Hutchins | c4a8243 | 2013-12-30 17:24:36 +0000 | [diff] [blame] | 223 | virtual void writeASTVisitorTraversal(raw_ostream &OS) const {} | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 224 | virtual void writeCloneArgs(raw_ostream &OS) const = 0; | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 225 | virtual void writeTemplateInstantiationArgs(raw_ostream &OS) const = 0; | 
| Daniel Dunbar | dc51baa | 2012-02-10 06:00:29 +0000 | [diff] [blame] | 226 | virtual void writeTemplateInstantiation(raw_ostream &OS) const {} | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 227 | virtual void writeCtorBody(raw_ostream &OS) const {} | 
|  | 228 | virtual void writeCtorInitializers(raw_ostream &OS) const = 0; | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 229 | virtual void writeCtorDefaultInitializers(raw_ostream &OS) const = 0; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 230 | virtual void writeCtorParameters(raw_ostream &OS) const = 0; | 
|  | 231 | virtual void writeDeclarations(raw_ostream &OS) const = 0; | 
|  | 232 | virtual void writePCHReadArgs(raw_ostream &OS) const = 0; | 
|  | 233 | virtual void writePCHReadDecls(raw_ostream &OS) const = 0; | 
|  | 234 | virtual void writePCHWrite(raw_ostream &OS) const = 0; | 
| Aaron Ballman | 48a533d | 2018-02-27 23:49:28 +0000 | [diff] [blame] | 235 | virtual std::string getIsOmitted() const { return "false"; } | 
| Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 236 | virtual void writeValue(raw_ostream &OS) const = 0; | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 237 | virtual void writeDump(raw_ostream &OS) const = 0; | 
|  | 238 | virtual void writeDumpChildren(raw_ostream &OS) const {} | 
| Richard Trieu | de5cc7d | 2013-01-31 01:44:26 +0000 | [diff] [blame] | 239 | virtual void writeHasChildren(raw_ostream &OS) const { OS << "false"; } | 
| Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 240 |  | 
|  | 241 | virtual bool isEnumArg() const { return false; } | 
| DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 242 | virtual bool isVariadicEnumArg() const { return false; } | 
| Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 243 | virtual bool isVariadic() const { return false; } | 
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 244 |  | 
|  | 245 | virtual void writeImplicitCtorArgs(raw_ostream &OS) const { | 
|  | 246 | OS << getUpperName(); | 
|  | 247 | } | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 248 | }; | 
|  | 249 |  | 
|  | 250 | class SimpleArgument : public Argument { | 
|  | 251 | std::string type; | 
|  | 252 |  | 
|  | 253 | public: | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 254 | SimpleArgument(const Record &Arg, StringRef Attr, std::string T) | 
| Benjamin Kramer | cfeacf5 | 2016-05-27 14:27:13 +0000 | [diff] [blame] | 255 | : Argument(Arg, Attr), type(std::move(T)) {} | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 256 |  | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 257 | std::string getType() const { return type; } | 
|  | 258 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 259 | void writeAccessors(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 260 | OS << "  " << type << " get" << getUpperName() << "() const {\n"; | 
|  | 261 | OS << "    return " << getLowerName() << ";\n"; | 
|  | 262 | OS << "  }"; | 
|  | 263 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 264 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 265 | void writeCloneArgs(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 266 | OS << getLowerName(); | 
|  | 267 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 268 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 269 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 270 | OS << "A->get" << getUpperName() << "()"; | 
|  | 271 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 272 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 273 | void writeCtorInitializers(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 274 | OS << getLowerName() << "(" << getUpperName() << ")"; | 
|  | 275 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 276 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 277 | void writeCtorDefaultInitializers(raw_ostream &OS) const override { | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 278 | OS << getLowerName() << "()"; | 
|  | 279 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 280 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 281 | void writeCtorParameters(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 282 | OS << type << " " << getUpperName(); | 
|  | 283 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 284 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 285 | void writeDeclarations(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 286 | OS << type << " " << getLowerName() << ";"; | 
|  | 287 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 288 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 289 | void writePCHReadDecls(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 290 | std::string read = ReadPCHRecord(type); | 
|  | 291 | OS << "    " << type << " " << getLowerName() << " = " << read << ";\n"; | 
|  | 292 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 293 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 294 | void writePCHReadArgs(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 295 | OS << getLowerName(); | 
|  | 296 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 297 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 298 | void writePCHWrite(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 299 | OS << "    " << WritePCHRecord(type, "SA->get" + | 
|  | 300 | std::string(getUpperName()) + "()"); | 
|  | 301 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 302 |  | 
| Aaron Ballman | 48a533d | 2018-02-27 23:49:28 +0000 | [diff] [blame] | 303 | std::string getIsOmitted() const override { | 
|  | 304 | if (type == "IdentifierInfo *") | 
|  | 305 | return "!get" + getUpperName().str() + "()"; | 
| Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 306 | if (type == "ParamIdx") | 
|  | 307 | return "!get" + getUpperName().str() + "().isValid()"; | 
| Aaron Ballman | 48a533d | 2018-02-27 23:49:28 +0000 | [diff] [blame] | 308 | return "false"; | 
|  | 309 | } | 
|  | 310 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 311 | void writeValue(raw_ostream &OS) const override { | 
| Aaron Ballman | 48a533d | 2018-02-27 23:49:28 +0000 | [diff] [blame] | 312 | if (type == "FunctionDecl *") | 
| Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 313 | OS << "\" << get" << getUpperName() | 
|  | 314 | << "()->getNameInfo().getAsString() << \""; | 
| Aaron Ballman | 48a533d | 2018-02-27 23:49:28 +0000 | [diff] [blame] | 315 | else if (type == "IdentifierInfo *") | 
|  | 316 | // Some non-optional (comma required) identifier arguments can be the | 
|  | 317 | // empty string but are then recorded as a nullptr. | 
|  | 318 | OS << "\" << (get" << getUpperName() << "() ? get" << getUpperName() | 
|  | 319 | << "()->getName() : \"\") << \""; | 
|  | 320 | else if (type == "TypeSourceInfo *") | 
| Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 321 | OS << "\" << get" << getUpperName() << "().getAsString() << \""; | 
| Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 322 | else if (type == "ParamIdx") | 
|  | 323 | OS << "\" << get" << getUpperName() << "().getSourceIndex() << \""; | 
| Aaron Ballman | 48a533d | 2018-02-27 23:49:28 +0000 | [diff] [blame] | 324 | else | 
| Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 325 | OS << "\" << get" << getUpperName() << "() << \""; | 
| Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 326 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 327 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 328 | void writeDump(raw_ostream &OS) const override { | 
| Argyrios Kyrtzidis | a7233bd | 2017-05-24 00:46:27 +0000 | [diff] [blame] | 329 | if (type == "FunctionDecl *" || type == "NamedDecl *") { | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 330 | OS << "    OS << \" \";\n"; | 
|  | 331 | OS << "    dumpBareDeclRef(SA->get" << getUpperName() << "());\n"; | 
|  | 332 | } else if (type == "IdentifierInfo *") { | 
| Aaron Ballman | 48a533d | 2018-02-27 23:49:28 +0000 | [diff] [blame] | 333 | // Some non-optional (comma required) identifier arguments can be the | 
|  | 334 | // empty string but are then recorded as a nullptr. | 
|  | 335 | OS << "    if (SA->get" << getUpperName() << "())\n" | 
|  | 336 | << "      OS << \" \" << SA->get" << getUpperName() | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 337 | << "()->getName();\n"; | 
| Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 338 | } else if (type == "TypeSourceInfo *") { | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 339 | OS << "    OS << \" \" << SA->get" << getUpperName() | 
|  | 340 | << "().getAsString();\n"; | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 341 | } else if (type == "bool") { | 
|  | 342 | OS << "    if (SA->get" << getUpperName() << "()) OS << \" " | 
|  | 343 | << getUpperName() << "\";\n"; | 
|  | 344 | } else if (type == "int" || type == "unsigned") { | 
|  | 345 | OS << "    OS << \" \" << SA->get" << getUpperName() << "();\n"; | 
| Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 346 | } else if (type == "ParamIdx") { | 
|  | 347 | if (isOptional()) | 
|  | 348 | OS << "    if (SA->get" << getUpperName() << "().isValid())\n  "; | 
|  | 349 | OS << "    OS << \" \" << SA->get" << getUpperName() | 
|  | 350 | << "().getSourceIndex();\n"; | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 351 | } else { | 
|  | 352 | llvm_unreachable("Unknown SimpleArgument type!"); | 
|  | 353 | } | 
|  | 354 | } | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 355 | }; | 
|  | 356 |  | 
| Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 357 | class DefaultSimpleArgument : public SimpleArgument { | 
|  | 358 | int64_t Default; | 
|  | 359 |  | 
|  | 360 | public: | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 361 | DefaultSimpleArgument(const Record &Arg, StringRef Attr, | 
| Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 362 | std::string T, int64_t Default) | 
|  | 363 | : SimpleArgument(Arg, Attr, T), Default(Default) {} | 
|  | 364 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 365 | void writeAccessors(raw_ostream &OS) const override { | 
| Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 366 | SimpleArgument::writeAccessors(OS); | 
|  | 367 |  | 
|  | 368 | OS << "\n\n  static const " << getType() << " Default" << getUpperName() | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 369 | << " = "; | 
|  | 370 | if (getType() == "bool") | 
|  | 371 | OS << (Default != 0 ? "true" : "false"); | 
|  | 372 | else | 
|  | 373 | OS << Default; | 
|  | 374 | OS << ";"; | 
| Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 375 | } | 
|  | 376 | }; | 
|  | 377 |  | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 378 | class StringArgument : public Argument { | 
|  | 379 | public: | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 380 | StringArgument(const Record &Arg, StringRef Attr) | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 381 | : Argument(Arg, Attr) | 
|  | 382 | {} | 
|  | 383 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 384 | void writeAccessors(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 385 | OS << "  llvm::StringRef get" << getUpperName() << "() const {\n"; | 
|  | 386 | OS << "    return llvm::StringRef(" << getLowerName() << ", " | 
|  | 387 | << getLowerName() << "Length);\n"; | 
|  | 388 | OS << "  }\n"; | 
|  | 389 | OS << "  unsigned get" << getUpperName() << "Length() const {\n"; | 
|  | 390 | OS << "    return " << getLowerName() << "Length;\n"; | 
|  | 391 | OS << "  }\n"; | 
|  | 392 | OS << "  void set" << getUpperName() | 
|  | 393 | << "(ASTContext &C, llvm::StringRef S) {\n"; | 
|  | 394 | OS << "    " << getLowerName() << "Length = S.size();\n"; | 
|  | 395 | OS << "    this->" << getLowerName() << " = new (C, 1) char [" | 
|  | 396 | << getLowerName() << "Length];\n"; | 
| Chandler Carruth | 38a45cc | 2015-08-04 03:53:01 +0000 | [diff] [blame] | 397 | OS << "    if (!S.empty())\n"; | 
|  | 398 | OS << "      std::memcpy(this->" << getLowerName() << ", S.data(), " | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 399 | << getLowerName() << "Length);\n"; | 
|  | 400 | OS << "  }"; | 
|  | 401 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 402 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 403 | void writeCloneArgs(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 404 | OS << "get" << getUpperName() << "()"; | 
|  | 405 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 406 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 407 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 408 | OS << "A->get" << getUpperName() << "()"; | 
|  | 409 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 410 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 411 | void writeCtorBody(raw_ostream &OS) const override { | 
| Chandler Carruth | 38a45cc | 2015-08-04 03:53:01 +0000 | [diff] [blame] | 412 | OS << "      if (!" << getUpperName() << ".empty())\n"; | 
|  | 413 | OS << "        std::memcpy(" << getLowerName() << ", " << getUpperName() | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 414 | << ".data(), " << getLowerName() << "Length);\n"; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 415 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 416 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 417 | void writeCtorInitializers(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 418 | OS << getLowerName() << "Length(" << getUpperName() << ".size())," | 
|  | 419 | << getLowerName() << "(new (Ctx, 1) char[" << getLowerName() | 
|  | 420 | << "Length])"; | 
|  | 421 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 422 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 423 | void writeCtorDefaultInitializers(raw_ostream &OS) const override { | 
| Hans Wennborg | 59dbe86 | 2015-09-29 20:56:43 +0000 | [diff] [blame] | 424 | OS << getLowerName() << "Length(0)," << getLowerName() << "(nullptr)"; | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 425 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 426 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 427 | void writeCtorParameters(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 428 | OS << "llvm::StringRef " << getUpperName(); | 
|  | 429 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 430 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 431 | void writeDeclarations(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 432 | OS << "unsigned " << getLowerName() << "Length;\n"; | 
|  | 433 | OS << "char *" << getLowerName() << ";"; | 
|  | 434 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 435 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 436 | void writePCHReadDecls(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 437 | OS << "    std::string " << getLowerName() | 
| David L. Jones | 267b884 | 2017-01-24 01:04:30 +0000 | [diff] [blame] | 438 | << "= Record.readString();\n"; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 439 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 440 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 441 | void writePCHReadArgs(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 442 | OS << getLowerName(); | 
|  | 443 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 444 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 445 | void writePCHWrite(raw_ostream &OS) const override { | 
| Richard Smith | 290d801 | 2016-04-06 17:06:00 +0000 | [diff] [blame] | 446 | OS << "    Record.AddString(SA->get" << getUpperName() << "());\n"; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 447 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 448 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 449 | void writeValue(raw_ostream &OS) const override { | 
| Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 450 | OS << "\\\"\" << get" << getUpperName() << "() << \"\\\""; | 
|  | 451 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 452 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 453 | void writeDump(raw_ostream &OS) const override { | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 454 | OS << "    OS << \" \\\"\" << SA->get" << getUpperName() | 
|  | 455 | << "() << \"\\\"\";\n"; | 
|  | 456 | } | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 457 | }; | 
|  | 458 |  | 
|  | 459 | class AlignedArgument : public Argument { | 
|  | 460 | public: | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 461 | AlignedArgument(const Record &Arg, StringRef Attr) | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 462 | : Argument(Arg, Attr) | 
|  | 463 | {} | 
|  | 464 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 465 | void writeAccessors(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 466 | OS << "  bool is" << getUpperName() << "Dependent() const;\n"; | 
|  | 467 |  | 
|  | 468 | OS << "  unsigned get" << getUpperName() << "(ASTContext &Ctx) const;\n"; | 
|  | 469 |  | 
|  | 470 | OS << "  bool is" << getUpperName() << "Expr() const {\n"; | 
|  | 471 | OS << "    return is" << getLowerName() << "Expr;\n"; | 
|  | 472 | OS << "  }\n"; | 
|  | 473 |  | 
|  | 474 | OS << "  Expr *get" << getUpperName() << "Expr() const {\n"; | 
|  | 475 | OS << "    assert(is" << getLowerName() << "Expr);\n"; | 
|  | 476 | OS << "    return " << getLowerName() << "Expr;\n"; | 
|  | 477 | OS << "  }\n"; | 
|  | 478 |  | 
|  | 479 | OS << "  TypeSourceInfo *get" << getUpperName() << "Type() const {\n"; | 
|  | 480 | OS << "    assert(!is" << getLowerName() << "Expr);\n"; | 
|  | 481 | OS << "    return " << getLowerName() << "Type;\n"; | 
|  | 482 | OS << "  }"; | 
|  | 483 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 484 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 485 | void writeAccessorDefinitions(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 486 | OS << "bool " << getAttrName() << "Attr::is" << getUpperName() | 
|  | 487 | << "Dependent() const {\n"; | 
|  | 488 | OS << "  if (is" << getLowerName() << "Expr)\n"; | 
|  | 489 | OS << "    return " << getLowerName() << "Expr && (" << getLowerName() | 
|  | 490 | << "Expr->isValueDependent() || " << getLowerName() | 
|  | 491 | << "Expr->isTypeDependent());\n"; | 
|  | 492 | OS << "  else\n"; | 
|  | 493 | OS << "    return " << getLowerName() | 
|  | 494 | << "Type->getType()->isDependentType();\n"; | 
|  | 495 | OS << "}\n"; | 
|  | 496 |  | 
|  | 497 | // FIXME: Do not do the calculation here | 
|  | 498 | // FIXME: Handle types correctly | 
|  | 499 | // A null pointer means maximum alignment | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 500 | OS << "unsigned " << getAttrName() << "Attr::get" << getUpperName() | 
|  | 501 | << "(ASTContext &Ctx) const {\n"; | 
|  | 502 | OS << "  assert(!is" << getUpperName() << "Dependent());\n"; | 
|  | 503 | OS << "  if (is" << getLowerName() << "Expr)\n"; | 
| Ulrich Weigand | ca3cb7f | 2015-04-21 17:29:35 +0000 | [diff] [blame] | 504 | OS << "    return " << getLowerName() << "Expr ? " << getLowerName() | 
|  | 505 | << "Expr->EvaluateKnownConstInt(Ctx).getZExtValue()" | 
|  | 506 | << " * Ctx.getCharWidth() : " | 
|  | 507 | << "Ctx.getTargetDefaultAlignForAttributeAligned();\n"; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 508 | OS << "  else\n"; | 
|  | 509 | OS << "    return 0; // FIXME\n"; | 
|  | 510 | OS << "}\n"; | 
|  | 511 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 512 |  | 
| Richard Smith | f26d551 | 2017-08-15 22:58:45 +0000 | [diff] [blame] | 513 | void writeASTVisitorTraversal(raw_ostream &OS) const override { | 
|  | 514 | StringRef Name = getUpperName(); | 
|  | 515 | OS << "  if (A->is" << Name << "Expr()) {\n" | 
|  | 516 | << "    if (!getDerived().TraverseStmt(A->get" << Name << "Expr()))\n" | 
|  | 517 | << "      return false;\n" | 
|  | 518 | << "  } else if (auto *TSI = A->get" << Name << "Type()) {\n" | 
|  | 519 | << "    if (!getDerived().TraverseTypeLoc(TSI->getTypeLoc()))\n" | 
|  | 520 | << "      return false;\n" | 
|  | 521 | << "  }\n"; | 
|  | 522 | } | 
|  | 523 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 524 | void writeCloneArgs(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 525 | OS << "is" << getLowerName() << "Expr, is" << getLowerName() | 
|  | 526 | << "Expr ? static_cast<void*>(" << getLowerName() | 
|  | 527 | << "Expr) : " << getLowerName() | 
|  | 528 | << "Type"; | 
|  | 529 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 530 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 531 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 532 | // FIXME: move the definition in Sema::InstantiateAttrs to here. | 
|  | 533 | // In the meantime, aligned attributes are cloned. | 
|  | 534 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 535 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 536 | void writeCtorBody(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 537 | OS << "    if (is" << getLowerName() << "Expr)\n"; | 
|  | 538 | OS << "       " << getLowerName() << "Expr = reinterpret_cast<Expr *>(" | 
|  | 539 | << getUpperName() << ");\n"; | 
|  | 540 | OS << "    else\n"; | 
|  | 541 | OS << "       " << getLowerName() | 
|  | 542 | << "Type = reinterpret_cast<TypeSourceInfo *>(" << getUpperName() | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 543 | << ");\n"; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 544 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 545 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 546 | void writeCtorInitializers(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 547 | OS << "is" << getLowerName() << "Expr(Is" << getUpperName() << "Expr)"; | 
|  | 548 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 549 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 550 | void writeCtorDefaultInitializers(raw_ostream &OS) const override { | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 551 | OS << "is" << getLowerName() << "Expr(false)"; | 
|  | 552 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 553 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 554 | void writeCtorParameters(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 555 | OS << "bool Is" << getUpperName() << "Expr, void *" << getUpperName(); | 
|  | 556 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 557 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 558 | void writeImplicitCtorArgs(raw_ostream &OS) const override { | 
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 559 | OS << "Is" << getUpperName() << "Expr, " << getUpperName(); | 
|  | 560 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 561 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 562 | void writeDeclarations(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 563 | OS << "bool is" << getLowerName() << "Expr;\n"; | 
|  | 564 | OS << "union {\n"; | 
|  | 565 | OS << "Expr *" << getLowerName() << "Expr;\n"; | 
|  | 566 | OS << "TypeSourceInfo *" << getLowerName() << "Type;\n"; | 
|  | 567 | OS << "};"; | 
|  | 568 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 569 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 570 | void writePCHReadArgs(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 571 | OS << "is" << getLowerName() << "Expr, " << getLowerName() << "Ptr"; | 
|  | 572 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 573 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 574 | void writePCHReadDecls(raw_ostream &OS) const override { | 
| David L. Jones | 267b884 | 2017-01-24 01:04:30 +0000 | [diff] [blame] | 575 | OS << "    bool is" << getLowerName() << "Expr = Record.readInt();\n"; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 576 | OS << "    void *" << getLowerName() << "Ptr;\n"; | 
|  | 577 | OS << "    if (is" << getLowerName() << "Expr)\n"; | 
| David L. Jones | 267b884 | 2017-01-24 01:04:30 +0000 | [diff] [blame] | 578 | OS << "      " << getLowerName() << "Ptr = Record.readExpr();\n"; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 579 | OS << "    else\n"; | 
|  | 580 | OS << "      " << getLowerName() | 
| David L. Jones | 267b884 | 2017-01-24 01:04:30 +0000 | [diff] [blame] | 581 | << "Ptr = Record.getTypeSourceInfo();\n"; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 582 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 583 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 584 | void writePCHWrite(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 585 | OS << "    Record.push_back(SA->is" << getUpperName() << "Expr());\n"; | 
|  | 586 | OS << "    if (SA->is" << getUpperName() << "Expr())\n"; | 
| Richard Smith | 290d801 | 2016-04-06 17:06:00 +0000 | [diff] [blame] | 587 | OS << "      Record.AddStmt(SA->get" << getUpperName() << "Expr());\n"; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 588 | OS << "    else\n"; | 
| Richard Smith | 290d801 | 2016-04-06 17:06:00 +0000 | [diff] [blame] | 589 | OS << "      Record.AddTypeSourceInfo(SA->get" << getUpperName() | 
|  | 590 | << "Type());\n"; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 591 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 592 |  | 
| Aaron Ballman | 48a533d | 2018-02-27 23:49:28 +0000 | [diff] [blame] | 593 | std::string getIsOmitted() const override { | 
|  | 594 | return "!is" + getLowerName().str() + "Expr || !" + getLowerName().str() | 
|  | 595 | + "Expr"; | 
|  | 596 | } | 
|  | 597 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 598 | void writeValue(raw_ostream &OS) const override { | 
| Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 599 | OS << "\";\n"; | 
| Aaron Ballman | 48a533d | 2018-02-27 23:49:28 +0000 | [diff] [blame] | 600 | OS << "    " << getLowerName() | 
|  | 601 | << "Expr->printPretty(OS, nullptr, Policy);\n"; | 
| Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 602 | OS << "    OS << \""; | 
| Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 603 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 604 |  | 
| Stephen Kelly | db8fac1 | 2019-01-11 19:16:01 +0000 | [diff] [blame] | 605 | void writeDump(raw_ostream &OS) const override { | 
|  | 606 | OS << "    if (!SA->is" << getUpperName() << "Expr())\n"; | 
|  | 607 | OS << "      dumpType(SA->get" << getUpperName() | 
|  | 608 | << "Type()->getType());\n"; | 
|  | 609 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 610 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 611 | void writeDumpChildren(raw_ostream &OS) const override { | 
| Richard Smith | f751445 | 2014-10-30 21:02:37 +0000 | [diff] [blame] | 612 | OS << "    if (SA->is" << getUpperName() << "Expr())\n"; | 
| Stephen Kelly | 6d110d6 | 2019-01-30 19:49:49 +0000 | [diff] [blame] | 613 | OS << "      Visit(SA->get" << getUpperName() << "Expr());\n"; | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 614 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 615 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 616 | void writeHasChildren(raw_ostream &OS) const override { | 
| Richard Trieu | de5cc7d | 2013-01-31 01:44:26 +0000 | [diff] [blame] | 617 | OS << "SA->is" << getUpperName() << "Expr()"; | 
|  | 618 | } | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 619 | }; | 
|  | 620 |  | 
|  | 621 | class VariadicArgument : public Argument { | 
| Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 622 | std::string Type, ArgName, ArgSizeName, RangeName; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 623 |  | 
| Aaron Ballman | 25a2cb9 | 2014-09-15 15:14:13 +0000 | [diff] [blame] | 624 | protected: | 
|  | 625 | // Assumed to receive a parameter: raw_ostream OS. | 
|  | 626 | virtual void writeValueImpl(raw_ostream &OS) const { | 
|  | 627 | OS << "    OS << Val;\n"; | 
|  | 628 | } | 
| Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 629 | // Assumed to receive a parameter: raw_ostream OS. | 
|  | 630 | virtual void writeDumpImpl(raw_ostream &OS) const { | 
|  | 631 | OS << "      OS << \" \" << Val;\n"; | 
|  | 632 | } | 
| Aaron Ballman | 25a2cb9 | 2014-09-15 15:14:13 +0000 | [diff] [blame] | 633 |  | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 634 | public: | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 635 | VariadicArgument(const Record &Arg, StringRef Attr, std::string T) | 
| Benjamin Kramer | cfeacf5 | 2016-05-27 14:27:13 +0000 | [diff] [blame] | 636 | : Argument(Arg, Attr), Type(std::move(T)), | 
|  | 637 | ArgName(getLowerName().str() + "_"), ArgSizeName(ArgName + "Size"), | 
|  | 638 | RangeName(getLowerName()) {} | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 639 |  | 
| Benjamin Kramer | 1b58201 | 2016-02-13 18:11:49 +0000 | [diff] [blame] | 640 | const std::string &getType() const { return Type; } | 
|  | 641 | const std::string &getArgName() const { return ArgName; } | 
|  | 642 | const std::string &getArgSizeName() const { return ArgSizeName; } | 
| Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 643 | bool isVariadic() const override { return true; } | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 644 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 645 | void writeAccessors(raw_ostream &OS) const override { | 
| Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 646 | std::string IteratorType = getLowerName().str() + "_iterator"; | 
|  | 647 | std::string BeginFn = getLowerName().str() + "_begin()"; | 
|  | 648 | std::string EndFn = getLowerName().str() + "_end()"; | 
|  | 649 |  | 
|  | 650 | OS << "  typedef " << Type << "* " << IteratorType << ";\n"; | 
|  | 651 | OS << "  " << IteratorType << " " << BeginFn << " const {" | 
|  | 652 | << " return " << ArgName << "; }\n"; | 
|  | 653 | OS << "  " << IteratorType << " " << EndFn << " const {" | 
|  | 654 | << " return " << ArgName << " + " << ArgSizeName << "; }\n"; | 
|  | 655 | OS << "  unsigned " << getLowerName() << "_size() const {" | 
|  | 656 | << " return " << ArgSizeName << "; }\n"; | 
|  | 657 | OS << "  llvm::iterator_range<" << IteratorType << "> " << RangeName | 
|  | 658 | << "() const { return llvm::make_range(" << BeginFn << ", " << EndFn | 
|  | 659 | << "); }\n"; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 660 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 661 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 662 | void writeCloneArgs(raw_ostream &OS) const override { | 
| Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 663 | OS << ArgName << ", " << ArgSizeName; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 664 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 665 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 666 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 667 | // This isn't elegant, but we have to go through public methods... | 
|  | 668 | OS << "A->" << getLowerName() << "_begin(), " | 
|  | 669 | << "A->" << getLowerName() << "_size()"; | 
|  | 670 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 671 |  | 
| Richard Smith | f26d551 | 2017-08-15 22:58:45 +0000 | [diff] [blame] | 672 | void writeASTVisitorTraversal(raw_ostream &OS) const override { | 
|  | 673 | // FIXME: Traverse the elements. | 
|  | 674 | } | 
|  | 675 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 676 | void writeCtorBody(raw_ostream &OS) const override { | 
| Aaron Ballman | d6459e5 | 2014-05-01 15:21:03 +0000 | [diff] [blame] | 677 | OS << "    std::copy(" << getUpperName() << ", " << getUpperName() | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 678 | << " + " << ArgSizeName << ", " << ArgName << ");\n"; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 679 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 680 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 681 | void writeCtorInitializers(raw_ostream &OS) const override { | 
| Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 682 | OS << ArgSizeName << "(" << getUpperName() << "Size), " | 
|  | 683 | << ArgName << "(new (Ctx, 16) " << getType() << "[" | 
|  | 684 | << ArgSizeName << "])"; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 685 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 686 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 687 | void writeCtorDefaultInitializers(raw_ostream &OS) const override { | 
| Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 688 | OS << ArgSizeName << "(0), " << ArgName << "(nullptr)"; | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 689 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 690 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 691 | void writeCtorParameters(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 692 | OS << getType() << " *" << getUpperName() << ", unsigned " | 
|  | 693 | << getUpperName() << "Size"; | 
|  | 694 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 695 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 696 | void writeImplicitCtorArgs(raw_ostream &OS) const override { | 
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 697 | OS << getUpperName() << ", " << getUpperName() << "Size"; | 
|  | 698 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 699 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 700 | void writeDeclarations(raw_ostream &OS) const override { | 
| Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 701 | OS << "  unsigned " << ArgSizeName << ";\n"; | 
|  | 702 | OS << "  " << getType() << " *" << ArgName << ";"; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 703 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 704 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 705 | void writePCHReadDecls(raw_ostream &OS) const override { | 
| David L. Jones | 267b884 | 2017-01-24 01:04:30 +0000 | [diff] [blame] | 706 | OS << "    unsigned " << getLowerName() << "Size = Record.readInt();\n"; | 
| Richard Smith | 1997856 | 2016-05-18 00:16:51 +0000 | [diff] [blame] | 707 | OS << "    SmallVector<" << getType() << ", 4> " | 
|  | 708 | << getLowerName() << ";\n"; | 
|  | 709 | OS << "    " << getLowerName() << ".reserve(" << getLowerName() | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 710 | << "Size);\n"; | 
| Richard Smith | 1997856 | 2016-05-18 00:16:51 +0000 | [diff] [blame] | 711 |  | 
|  | 712 | // If we can't store the values in the current type (if it's something | 
|  | 713 | // like StringRef), store them in a different type and convert the | 
|  | 714 | // container afterwards. | 
|  | 715 | std::string StorageType = getStorageType(getType()); | 
|  | 716 | std::string StorageName = getLowerName(); | 
|  | 717 | if (StorageType != getType()) { | 
|  | 718 | StorageName += "Storage"; | 
|  | 719 | OS << "    SmallVector<" << StorageType << ", 4> " | 
|  | 720 | << StorageName << ";\n"; | 
|  | 721 | OS << "    " << StorageName << ".reserve(" << getLowerName() | 
|  | 722 | << "Size);\n"; | 
|  | 723 | } | 
|  | 724 |  | 
|  | 725 | OS << "    for (unsigned i = 0; i != " << getLowerName() << "Size; ++i)\n"; | 
| Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 726 | std::string read = ReadPCHRecord(Type); | 
| Richard Smith | 1997856 | 2016-05-18 00:16:51 +0000 | [diff] [blame] | 727 | OS << "      " << StorageName << ".push_back(" << read << ");\n"; | 
|  | 728 |  | 
|  | 729 | if (StorageType != getType()) { | 
|  | 730 | OS << "    for (unsigned i = 0; i != " << getLowerName() << "Size; ++i)\n"; | 
|  | 731 | OS << "      " << getLowerName() << ".push_back(" | 
|  | 732 | << StorageName << "[i]);\n"; | 
|  | 733 | } | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 734 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 735 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 736 | void writePCHReadArgs(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 737 | OS << getLowerName() << ".data(), " << getLowerName() << "Size"; | 
|  | 738 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 739 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 740 | void writePCHWrite(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 741 | OS << "    Record.push_back(SA->" << getLowerName() << "_size());\n"; | 
| Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 742 | OS << "    for (auto &Val : SA->" << RangeName << "())\n"; | 
|  | 743 | OS << "      " << WritePCHRecord(Type, "Val"); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 744 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 745 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 746 | void writeValue(raw_ostream &OS) const override { | 
| Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 747 | OS << "\";\n"; | 
|  | 748 | OS << "  bool isFirst = true;\n" | 
| Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 749 | << "  for (const auto &Val : " << RangeName << "()) {\n" | 
| Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 750 | << "    if (isFirst) isFirst = false;\n" | 
| Aaron Ballman | 25a2cb9 | 2014-09-15 15:14:13 +0000 | [diff] [blame] | 751 | << "    else OS << \", \";\n"; | 
|  | 752 | writeValueImpl(OS); | 
|  | 753 | OS << "  }\n"; | 
| Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 754 | OS << "  OS << \""; | 
|  | 755 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 756 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 757 | void writeDump(raw_ostream &OS) const override { | 
| Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 758 | OS << "    for (const auto &Val : SA->" << RangeName << "())\n"; | 
| Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 759 | writeDumpImpl(OS); | 
|  | 760 | } | 
|  | 761 | }; | 
|  | 762 |  | 
|  | 763 | class VariadicParamIdxArgument : public VariadicArgument { | 
|  | 764 | public: | 
|  | 765 | VariadicParamIdxArgument(const Record &Arg, StringRef Attr) | 
|  | 766 | : VariadicArgument(Arg, Attr, "ParamIdx") {} | 
|  | 767 |  | 
|  | 768 | public: | 
|  | 769 | void writeValueImpl(raw_ostream &OS) const override { | 
|  | 770 | OS << "    OS << Val.getSourceIndex();\n"; | 
|  | 771 | } | 
|  | 772 |  | 
|  | 773 | void writeDumpImpl(raw_ostream &OS) const override { | 
|  | 774 | OS << "      OS << \" \" << Val.getSourceIndex();\n"; | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 775 | } | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 776 | }; | 
|  | 777 |  | 
| Johannes Doerfert | ac991bb | 2019-01-19 05:36:54 +0000 | [diff] [blame] | 778 | struct VariadicParamOrParamIdxArgument : public VariadicArgument { | 
|  | 779 | VariadicParamOrParamIdxArgument(const Record &Arg, StringRef Attr) | 
|  | 780 | : VariadicArgument(Arg, Attr, "int") {} | 
|  | 781 | }; | 
|  | 782 |  | 
| Reid Kleckner | f526b948 | 2014-02-12 18:22:18 +0000 | [diff] [blame] | 783 | // Unique the enums, but maintain the original declaration ordering. | 
| Craig Topper | 0064858 | 2017-05-31 19:01:22 +0000 | [diff] [blame] | 784 | std::vector<StringRef> | 
|  | 785 | uniqueEnumsInOrder(const std::vector<StringRef> &enums) { | 
|  | 786 | std::vector<StringRef> uniques; | 
| George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 787 | SmallDenseSet<StringRef, 8> unique_set; | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 788 | for (const auto &i : enums) { | 
| George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 789 | if (unique_set.insert(i).second) | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 790 | uniques.push_back(i); | 
| Reid Kleckner | f526b948 | 2014-02-12 18:22:18 +0000 | [diff] [blame] | 791 | } | 
|  | 792 | return uniques; | 
|  | 793 | } | 
|  | 794 |  | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 795 | class EnumArgument : public Argument { | 
|  | 796 | std::string type; | 
| Craig Topper | 0064858 | 2017-05-31 19:01:22 +0000 | [diff] [blame] | 797 | std::vector<StringRef> values, enums, uniques; | 
|  | 798 |  | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 799 | public: | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 800 | EnumArgument(const Record &Arg, StringRef Attr) | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 801 | : Argument(Arg, Attr), type(Arg.getValueAsString("Type")), | 
| Aaron Ballman | 0e468c0 | 2014-01-05 21:08:29 +0000 | [diff] [blame] | 802 | values(Arg.getValueAsListOfStrings("Values")), | 
|  | 803 | enums(Arg.getValueAsListOfStrings("Enums")), | 
| Reid Kleckner | f526b948 | 2014-02-12 18:22:18 +0000 | [diff] [blame] | 804 | uniques(uniqueEnumsInOrder(enums)) | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 805 | { | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 806 | // FIXME: Emit a proper error | 
|  | 807 | assert(!uniques.empty()); | 
|  | 808 | } | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 809 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 810 | bool isEnumArg() const override { return true; } | 
| Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 811 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 812 | void writeAccessors(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 813 | OS << "  " << type << " get" << getUpperName() << "() const {\n"; | 
|  | 814 | OS << "    return " << getLowerName() << ";\n"; | 
|  | 815 | OS << "  }"; | 
|  | 816 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 817 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 818 | void writeCloneArgs(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 819 | OS << getLowerName(); | 
|  | 820 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 821 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 822 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 823 | OS << "A->get" << getUpperName() << "()"; | 
|  | 824 | } | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 825 | void writeCtorInitializers(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 826 | OS << getLowerName() << "(" << getUpperName() << ")"; | 
|  | 827 | } | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 828 | void writeCtorDefaultInitializers(raw_ostream &OS) const override { | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 829 | OS << getLowerName() << "(" << type << "(0))"; | 
|  | 830 | } | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 831 | void writeCtorParameters(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 832 | OS << type << " " << getUpperName(); | 
|  | 833 | } | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 834 | void writeDeclarations(raw_ostream &OS) const override { | 
| Eugene Zelenko | 5f02b77 | 2015-12-08 18:49:01 +0000 | [diff] [blame] | 835 | auto i = uniques.cbegin(), e = uniques.cend(); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 836 | // The last one needs to not have a comma. | 
|  | 837 | --e; | 
|  | 838 |  | 
|  | 839 | OS << "public:\n"; | 
|  | 840 | OS << "  enum " << type << " {\n"; | 
|  | 841 | for (; i != e; ++i) | 
|  | 842 | OS << "    " << *i << ",\n"; | 
|  | 843 | OS << "    " << *e << "\n"; | 
|  | 844 | OS << "  };\n"; | 
|  | 845 | OS << "private:\n"; | 
|  | 846 | OS << "  " << type << " " << getLowerName() << ";"; | 
|  | 847 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 848 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 849 | void writePCHReadDecls(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 850 | OS << "    " << getAttrName() << "Attr::" << type << " " << getLowerName() | 
|  | 851 | << "(static_cast<" << getAttrName() << "Attr::" << type | 
| David L. Jones | 267b884 | 2017-01-24 01:04:30 +0000 | [diff] [blame] | 852 | << ">(Record.readInt()));\n"; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 853 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 854 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 855 | void writePCHReadArgs(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 856 | OS << getLowerName(); | 
|  | 857 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 858 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 859 | void writePCHWrite(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 860 | OS << "Record.push_back(SA->get" << getUpperName() << "());\n"; | 
|  | 861 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 862 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 863 | void writeValue(raw_ostream &OS) const override { | 
| Aaron Ballman | 36d7910 | 2014-09-15 16:16:14 +0000 | [diff] [blame] | 864 | // FIXME: this isn't 100% correct -- some enum arguments require printing | 
|  | 865 | // as a string literal, while others require printing as an identifier. | 
|  | 866 | // Tablegen currently does not distinguish between the two forms. | 
| Aaron Ballman | 25a2cb9 | 2014-09-15 15:14:13 +0000 | [diff] [blame] | 867 | OS << "\\\"\" << " << getAttrName() << "Attr::Convert" << type << "ToStr(get" | 
|  | 868 | << getUpperName() << "()) << \"\\\""; | 
| Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 869 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 870 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 871 | void writeDump(raw_ostream &OS) const override { | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 872 | OS << "    switch(SA->get" << getUpperName() << "()) {\n"; | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 873 | for (const auto &I : uniques) { | 
|  | 874 | OS << "    case " << getAttrName() << "Attr::" << I << ":\n"; | 
|  | 875 | OS << "      OS << \" " << I << "\";\n"; | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 876 | OS << "      break;\n"; | 
|  | 877 | } | 
|  | 878 | OS << "    }\n"; | 
|  | 879 | } | 
| Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 880 |  | 
|  | 881 | void writeConversion(raw_ostream &OS) const { | 
|  | 882 | OS << "  static bool ConvertStrTo" << type << "(StringRef Val, "; | 
|  | 883 | OS << type << " &Out) {\n"; | 
|  | 884 | OS << "    Optional<" << type << "> R = llvm::StringSwitch<Optional<"; | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 885 | OS << type << ">>(Val)\n"; | 
| Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 886 | for (size_t I = 0; I < enums.size(); ++I) { | 
|  | 887 | OS << "      .Case(\"" << values[I] << "\", "; | 
|  | 888 | OS << getAttrName() << "Attr::" << enums[I] << ")\n"; | 
|  | 889 | } | 
|  | 890 | OS << "      .Default(Optional<" << type << ">());\n"; | 
|  | 891 | OS << "    if (R) {\n"; | 
|  | 892 | OS << "      Out = *R;\n      return true;\n    }\n"; | 
|  | 893 | OS << "    return false;\n"; | 
| Aaron Ballman | 25a2cb9 | 2014-09-15 15:14:13 +0000 | [diff] [blame] | 894 | OS << "  }\n\n"; | 
|  | 895 |  | 
|  | 896 | // Mapping from enumeration values back to enumeration strings isn't | 
|  | 897 | // trivial because some enumeration values have multiple named | 
|  | 898 | // enumerators, such as type_visibility(internal) and | 
|  | 899 | // type_visibility(hidden) both mapping to TypeVisibilityAttr::Hidden. | 
|  | 900 | OS << "  static const char *Convert" << type << "ToStr(" | 
|  | 901 | << type << " Val) {\n" | 
|  | 902 | << "    switch(Val) {\n"; | 
| George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 903 | SmallDenseSet<StringRef, 8> Uniques; | 
| Aaron Ballman | 25a2cb9 | 2014-09-15 15:14:13 +0000 | [diff] [blame] | 904 | for (size_t I = 0; I < enums.size(); ++I) { | 
|  | 905 | if (Uniques.insert(enums[I]).second) | 
|  | 906 | OS << "    case " << getAttrName() << "Attr::" << enums[I] | 
|  | 907 | << ": return \"" << values[I] << "\";\n"; | 
|  | 908 | } | 
|  | 909 | OS << "    }\n" | 
|  | 910 | << "    llvm_unreachable(\"No enumerator with that value\");\n" | 
|  | 911 | << "  }\n"; | 
| Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 912 | } | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 913 | }; | 
| DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 914 |  | 
|  | 915 | class VariadicEnumArgument: public VariadicArgument { | 
|  | 916 | std::string type, QualifiedTypeName; | 
| Craig Topper | 0064858 | 2017-05-31 19:01:22 +0000 | [diff] [blame] | 917 | std::vector<StringRef> values, enums, uniques; | 
| Aaron Ballman | 25a2cb9 | 2014-09-15 15:14:13 +0000 | [diff] [blame] | 918 |  | 
|  | 919 | protected: | 
|  | 920 | void writeValueImpl(raw_ostream &OS) const override { | 
| Aaron Ballman | 36d7910 | 2014-09-15 16:16:14 +0000 | [diff] [blame] | 921 | // FIXME: this isn't 100% correct -- some enum arguments require printing | 
|  | 922 | // as a string literal, while others require printing as an identifier. | 
|  | 923 | // Tablegen currently does not distinguish between the two forms. | 
| Aaron Ballman | 25a2cb9 | 2014-09-15 15:14:13 +0000 | [diff] [blame] | 924 | OS << "    OS << \"\\\"\" << " << getAttrName() << "Attr::Convert" << type | 
|  | 925 | << "ToStr(Val)" << "<< \"\\\"\";\n"; | 
|  | 926 | } | 
|  | 927 |  | 
| DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 928 | public: | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 929 | VariadicEnumArgument(const Record &Arg, StringRef Attr) | 
| DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 930 | : VariadicArgument(Arg, Attr, Arg.getValueAsString("Type")), | 
|  | 931 | type(Arg.getValueAsString("Type")), | 
| Aaron Ballman | 0e468c0 | 2014-01-05 21:08:29 +0000 | [diff] [blame] | 932 | values(Arg.getValueAsListOfStrings("Values")), | 
|  | 933 | enums(Arg.getValueAsListOfStrings("Enums")), | 
| Reid Kleckner | f526b948 | 2014-02-12 18:22:18 +0000 | [diff] [blame] | 934 | uniques(uniqueEnumsInOrder(enums)) | 
| DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 935 | { | 
| DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 936 | QualifiedTypeName = getAttrName().str() + "Attr::" + type; | 
|  | 937 |  | 
|  | 938 | // FIXME: Emit a proper error | 
|  | 939 | assert(!uniques.empty()); | 
|  | 940 | } | 
|  | 941 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 942 | bool isVariadicEnumArg() const override { return true; } | 
| DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 943 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 944 | void writeDeclarations(raw_ostream &OS) const override { | 
| Eugene Zelenko | 5f02b77 | 2015-12-08 18:49:01 +0000 | [diff] [blame] | 945 | auto i = uniques.cbegin(), e = uniques.cend(); | 
| DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 946 | // The last one needs to not have a comma. | 
|  | 947 | --e; | 
|  | 948 |  | 
|  | 949 | OS << "public:\n"; | 
|  | 950 | OS << "  enum " << type << " {\n"; | 
|  | 951 | for (; i != e; ++i) | 
|  | 952 | OS << "    " << *i << ",\n"; | 
|  | 953 | OS << "    " << *e << "\n"; | 
|  | 954 | OS << "  };\n"; | 
|  | 955 | OS << "private:\n"; | 
|  | 956 |  | 
|  | 957 | VariadicArgument::writeDeclarations(OS); | 
|  | 958 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 959 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 960 | void writeDump(raw_ostream &OS) const override { | 
| DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 961 | OS << "    for (" << getAttrName() << "Attr::" << getLowerName() | 
|  | 962 | << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->" | 
|  | 963 | << getLowerName() << "_end(); I != E; ++I) {\n"; | 
|  | 964 | OS << "      switch(*I) {\n"; | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 965 | for (const auto &UI : uniques) { | 
|  | 966 | OS << "    case " << getAttrName() << "Attr::" << UI << ":\n"; | 
|  | 967 | OS << "      OS << \" " << UI << "\";\n"; | 
| DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 968 | OS << "      break;\n"; | 
|  | 969 | } | 
|  | 970 | OS << "      }\n"; | 
|  | 971 | OS << "    }\n"; | 
|  | 972 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 973 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 974 | void writePCHReadDecls(raw_ostream &OS) const override { | 
| David L. Jones | 267b884 | 2017-01-24 01:04:30 +0000 | [diff] [blame] | 975 | OS << "    unsigned " << getLowerName() << "Size = Record.readInt();\n"; | 
| DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 976 | OS << "    SmallVector<" << QualifiedTypeName << ", 4> " << getLowerName() | 
|  | 977 | << ";\n"; | 
|  | 978 | OS << "    " << getLowerName() << ".reserve(" << getLowerName() | 
|  | 979 | << "Size);\n"; | 
|  | 980 | OS << "    for (unsigned i = " << getLowerName() << "Size; i; --i)\n"; | 
|  | 981 | OS << "      " << getLowerName() << ".push_back(" << "static_cast<" | 
| David L. Jones | 267b884 | 2017-01-24 01:04:30 +0000 | [diff] [blame] | 982 | << QualifiedTypeName << ">(Record.readInt()));\n"; | 
| DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 983 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 984 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 985 | void writePCHWrite(raw_ostream &OS) const override { | 
| DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 986 | OS << "    Record.push_back(SA->" << getLowerName() << "_size());\n"; | 
|  | 987 | OS << "    for (" << getAttrName() << "Attr::" << getLowerName() | 
|  | 988 | << "_iterator i = SA->" << getLowerName() << "_begin(), e = SA->" | 
|  | 989 | << getLowerName() << "_end(); i != e; ++i)\n"; | 
|  | 990 | OS << "      " << WritePCHRecord(QualifiedTypeName, "(*i)"); | 
|  | 991 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 992 |  | 
| DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 993 | void writeConversion(raw_ostream &OS) const { | 
|  | 994 | OS << "  static bool ConvertStrTo" << type << "(StringRef Val, "; | 
|  | 995 | OS << type << " &Out) {\n"; | 
|  | 996 | OS << "    Optional<" << type << "> R = llvm::StringSwitch<Optional<"; | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 997 | OS << type << ">>(Val)\n"; | 
| DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 998 | for (size_t I = 0; I < enums.size(); ++I) { | 
|  | 999 | OS << "      .Case(\"" << values[I] << "\", "; | 
|  | 1000 | OS << getAttrName() << "Attr::" << enums[I] << ")\n"; | 
|  | 1001 | } | 
|  | 1002 | OS << "      .Default(Optional<" << type << ">());\n"; | 
|  | 1003 | OS << "    if (R) {\n"; | 
|  | 1004 | OS << "      Out = *R;\n      return true;\n    }\n"; | 
|  | 1005 | OS << "    return false;\n"; | 
| Aaron Ballman | 25a2cb9 | 2014-09-15 15:14:13 +0000 | [diff] [blame] | 1006 | OS << "  }\n\n"; | 
|  | 1007 |  | 
|  | 1008 | OS << "  static const char *Convert" << type << "ToStr(" | 
|  | 1009 | << type << " Val) {\n" | 
|  | 1010 | << "    switch(Val) {\n"; | 
| George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 1011 | SmallDenseSet<StringRef, 8> Uniques; | 
| Aaron Ballman | 25a2cb9 | 2014-09-15 15:14:13 +0000 | [diff] [blame] | 1012 | for (size_t I = 0; I < enums.size(); ++I) { | 
|  | 1013 | if (Uniques.insert(enums[I]).second) | 
|  | 1014 | OS << "    case " << getAttrName() << "Attr::" << enums[I] | 
|  | 1015 | << ": return \"" << values[I] << "\";\n"; | 
|  | 1016 | } | 
|  | 1017 | OS << "    }\n" | 
|  | 1018 | << "    llvm_unreachable(\"No enumerator with that value\");\n" | 
|  | 1019 | << "  }\n"; | 
| DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1020 | } | 
|  | 1021 | }; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1022 |  | 
|  | 1023 | class VersionArgument : public Argument { | 
|  | 1024 | public: | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 1025 | VersionArgument(const Record &Arg, StringRef Attr) | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1026 | : Argument(Arg, Attr) | 
|  | 1027 | {} | 
|  | 1028 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1029 | void writeAccessors(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1030 | OS << "  VersionTuple get" << getUpperName() << "() const {\n"; | 
|  | 1031 | OS << "    return " << getLowerName() << ";\n"; | 
|  | 1032 | OS << "  }\n"; | 
|  | 1033 | OS << "  void set" << getUpperName() | 
|  | 1034 | << "(ASTContext &C, VersionTuple V) {\n"; | 
|  | 1035 | OS << "    " << getLowerName() << " = V;\n"; | 
|  | 1036 | OS << "  }"; | 
|  | 1037 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1038 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1039 | void writeCloneArgs(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1040 | OS << "get" << getUpperName() << "()"; | 
|  | 1041 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1042 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1043 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1044 | OS << "A->get" << getUpperName() << "()"; | 
|  | 1045 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1046 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1047 | void writeCtorInitializers(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1048 | OS << getLowerName() << "(" << getUpperName() << ")"; | 
|  | 1049 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1050 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1051 | void writeCtorDefaultInitializers(raw_ostream &OS) const override { | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 1052 | OS << getLowerName() << "()"; | 
|  | 1053 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1054 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1055 | void writeCtorParameters(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1056 | OS << "VersionTuple " << getUpperName(); | 
|  | 1057 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1058 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1059 | void writeDeclarations(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1060 | OS << "VersionTuple " << getLowerName() << ";\n"; | 
|  | 1061 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1062 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1063 | void writePCHReadDecls(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1064 | OS << "    VersionTuple " << getLowerName() | 
| David L. Jones | 267b884 | 2017-01-24 01:04:30 +0000 | [diff] [blame] | 1065 | << "= Record.readVersionTuple();\n"; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1066 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1067 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1068 | void writePCHReadArgs(raw_ostream &OS) const override { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1069 | OS << getLowerName(); | 
|  | 1070 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1071 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1072 | void writePCHWrite(raw_ostream &OS) const override { | 
| Richard Smith | 290d801 | 2016-04-06 17:06:00 +0000 | [diff] [blame] | 1073 | OS << "    Record.AddVersionTuple(SA->get" << getUpperName() << "());\n"; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1074 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1075 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1076 | void writeValue(raw_ostream &OS) const override { | 
| Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 1077 | OS << getLowerName() << "=\" << get" << getUpperName() << "() << \""; | 
|  | 1078 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1079 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1080 | void writeDump(raw_ostream &OS) const override { | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 1081 | OS << "    OS << \" \" << SA->get" << getUpperName() << "();\n"; | 
|  | 1082 | } | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1083 | }; | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1084 |  | 
|  | 1085 | class ExprArgument : public SimpleArgument { | 
|  | 1086 | public: | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 1087 | ExprArgument(const Record &Arg, StringRef Attr) | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1088 | : SimpleArgument(Arg, Attr, "Expr *") | 
|  | 1089 | {} | 
|  | 1090 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1091 | void writeASTVisitorTraversal(raw_ostream &OS) const override { | 
| DeLesley Hutchins | c4a8243 | 2013-12-30 17:24:36 +0000 | [diff] [blame] | 1092 | OS << "  if (!" | 
|  | 1093 | << "getDerived().TraverseStmt(A->get" << getUpperName() << "()))\n"; | 
|  | 1094 | OS << "    return false;\n"; | 
|  | 1095 | } | 
|  | 1096 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1097 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1098 | OS << "tempInst" << getUpperName(); | 
|  | 1099 | } | 
|  | 1100 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1101 | void writeTemplateInstantiation(raw_ostream &OS) const override { | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1102 | OS << "      " << getType() << " tempInst" << getUpperName() << ";\n"; | 
|  | 1103 | OS << "      {\n"; | 
|  | 1104 | OS << "        EnterExpressionEvaluationContext " | 
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 1105 | << "Unevaluated(S, Sema::ExpressionEvaluationContext::Unevaluated);\n"; | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1106 | OS << "        ExprResult " << "Result = S.SubstExpr(" | 
|  | 1107 | << "A->get" << getUpperName() << "(), TemplateArgs);\n"; | 
|  | 1108 | OS << "        tempInst" << getUpperName() << " = " | 
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1109 | << "Result.getAs<Expr>();\n"; | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1110 | OS << "      }\n"; | 
|  | 1111 | } | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 1112 |  | 
| Craig Topper | 3164f33 | 2014-03-11 03:39:26 +0000 | [diff] [blame] | 1113 | void writeDump(raw_ostream &OS) const override {} | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 1114 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1115 | void writeDumpChildren(raw_ostream &OS) const override { | 
| Stephen Kelly | 6d110d6 | 2019-01-30 19:49:49 +0000 | [diff] [blame] | 1116 | OS << "    Visit(SA->get" << getUpperName() << "());\n"; | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 1117 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1118 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1119 | void writeHasChildren(raw_ostream &OS) const override { OS << "true"; } | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1120 | }; | 
|  | 1121 |  | 
|  | 1122 | class VariadicExprArgument : public VariadicArgument { | 
|  | 1123 | public: | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 1124 | VariadicExprArgument(const Record &Arg, StringRef Attr) | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1125 | : VariadicArgument(Arg, Attr, "Expr *") | 
|  | 1126 | {} | 
|  | 1127 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1128 | void writeASTVisitorTraversal(raw_ostream &OS) const override { | 
| DeLesley Hutchins | c4a8243 | 2013-12-30 17:24:36 +0000 | [diff] [blame] | 1129 | OS << "  {\n"; | 
|  | 1130 | OS << "    " << getType() << " *I = A->" << getLowerName() | 
|  | 1131 | << "_begin();\n"; | 
|  | 1132 | OS << "    " << getType() << " *E = A->" << getLowerName() | 
|  | 1133 | << "_end();\n"; | 
|  | 1134 | OS << "    for (; I != E; ++I) {\n"; | 
|  | 1135 | OS << "      if (!getDerived().TraverseStmt(*I))\n"; | 
|  | 1136 | OS << "        return false;\n"; | 
|  | 1137 | OS << "    }\n"; | 
|  | 1138 | OS << "  }\n"; | 
|  | 1139 | } | 
|  | 1140 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1141 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1142 | OS << "tempInst" << getUpperName() << ", " | 
|  | 1143 | << "A->" << getLowerName() << "_size()"; | 
|  | 1144 | } | 
|  | 1145 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1146 | void writeTemplateInstantiation(raw_ostream &OS) const override { | 
| Eugene Zelenko | 5f02b77 | 2015-12-08 18:49:01 +0000 | [diff] [blame] | 1147 | OS << "      auto *tempInst" << getUpperName() | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1148 | << " = new (C, 16) " << getType() | 
|  | 1149 | << "[A->" << getLowerName() << "_size()];\n"; | 
|  | 1150 | OS << "      {\n"; | 
|  | 1151 | OS << "        EnterExpressionEvaluationContext " | 
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 1152 | << "Unevaluated(S, Sema::ExpressionEvaluationContext::Unevaluated);\n"; | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1153 | OS << "        " << getType() << " *TI = tempInst" << getUpperName() | 
|  | 1154 | << ";\n"; | 
|  | 1155 | OS << "        " << getType() << " *I = A->" << getLowerName() | 
|  | 1156 | << "_begin();\n"; | 
|  | 1157 | OS << "        " << getType() << " *E = A->" << getLowerName() | 
|  | 1158 | << "_end();\n"; | 
|  | 1159 | OS << "        for (; I != E; ++I, ++TI) {\n"; | 
|  | 1160 | OS << "          ExprResult Result = S.SubstExpr(*I, TemplateArgs);\n"; | 
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1161 | OS << "          *TI = Result.getAs<Expr>();\n"; | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1162 | OS << "        }\n"; | 
|  | 1163 | OS << "      }\n"; | 
|  | 1164 | } | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 1165 |  | 
| Craig Topper | 3164f33 | 2014-03-11 03:39:26 +0000 | [diff] [blame] | 1166 | void writeDump(raw_ostream &OS) const override {} | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 1167 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1168 | void writeDumpChildren(raw_ostream &OS) const override { | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 1169 | OS << "    for (" << getAttrName() << "Attr::" << getLowerName() | 
|  | 1170 | << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->" | 
| Richard Smith | f751445 | 2014-10-30 21:02:37 +0000 | [diff] [blame] | 1171 | << getLowerName() << "_end(); I != E; ++I)\n"; | 
| Stephen Kelly | 6d110d6 | 2019-01-30 19:49:49 +0000 | [diff] [blame] | 1172 | OS << "      Visit(*I);\n"; | 
| Richard Trieu | de5cc7d | 2013-01-31 01:44:26 +0000 | [diff] [blame] | 1173 | } | 
|  | 1174 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1175 | void writeHasChildren(raw_ostream &OS) const override { | 
| Richard Trieu | de5cc7d | 2013-01-31 01:44:26 +0000 | [diff] [blame] | 1176 | OS << "SA->" << getLowerName() << "_begin() != " | 
|  | 1177 | << "SA->" << getLowerName() << "_end()"; | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 1178 | } | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 1179 | }; | 
| Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1180 |  | 
| Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 1181 | class VariadicIdentifierArgument : public VariadicArgument { | 
|  | 1182 | public: | 
|  | 1183 | VariadicIdentifierArgument(const Record &Arg, StringRef Attr) | 
|  | 1184 | : VariadicArgument(Arg, Attr, "IdentifierInfo *") | 
|  | 1185 | {} | 
|  | 1186 | }; | 
|  | 1187 |  | 
| Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 1188 | class VariadicStringArgument : public VariadicArgument { | 
|  | 1189 | public: | 
|  | 1190 | VariadicStringArgument(const Record &Arg, StringRef Attr) | 
| Benjamin Kramer | 1b58201 | 2016-02-13 18:11:49 +0000 | [diff] [blame] | 1191 | : VariadicArgument(Arg, Attr, "StringRef") | 
| Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 1192 | {} | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1193 |  | 
| Benjamin Kramer | 1b58201 | 2016-02-13 18:11:49 +0000 | [diff] [blame] | 1194 | void writeCtorBody(raw_ostream &OS) const override { | 
|  | 1195 | OS << "    for (size_t I = 0, E = " << getArgSizeName() << "; I != E;\n" | 
|  | 1196 | "         ++I) {\n" | 
|  | 1197 | "      StringRef Ref = " << getUpperName() << "[I];\n" | 
|  | 1198 | "      if (!Ref.empty()) {\n" | 
|  | 1199 | "        char *Mem = new (Ctx, 1) char[Ref.size()];\n" | 
|  | 1200 | "        std::memcpy(Mem, Ref.data(), Ref.size());\n" | 
|  | 1201 | "        " << getArgName() << "[I] = StringRef(Mem, Ref.size());\n" | 
|  | 1202 | "      }\n" | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1203 | "    }\n"; | 
| Benjamin Kramer | 1b58201 | 2016-02-13 18:11:49 +0000 | [diff] [blame] | 1204 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1205 |  | 
| Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 1206 | void writeValueImpl(raw_ostream &OS) const override { | 
|  | 1207 | OS << "    OS << \"\\\"\" << Val << \"\\\"\";\n"; | 
|  | 1208 | } | 
|  | 1209 | }; | 
|  | 1210 |  | 
| Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1211 | class TypeArgument : public SimpleArgument { | 
|  | 1212 | public: | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 1213 | TypeArgument(const Record &Arg, StringRef Attr) | 
| Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1214 | : SimpleArgument(Arg, Attr, "TypeSourceInfo *") | 
|  | 1215 | {} | 
|  | 1216 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1217 | void writeAccessors(raw_ostream &OS) const override { | 
| Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1218 | OS << "  QualType get" << getUpperName() << "() const {\n"; | 
|  | 1219 | OS << "    return " << getLowerName() << "->getType();\n"; | 
|  | 1220 | OS << "  }"; | 
|  | 1221 | OS << "  " << getType() << " get" << getUpperName() << "Loc() const {\n"; | 
|  | 1222 | OS << "    return " << getLowerName() << ";\n"; | 
|  | 1223 | OS << "  }"; | 
|  | 1224 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1225 |  | 
| Richard Smith | f26d551 | 2017-08-15 22:58:45 +0000 | [diff] [blame] | 1226 | void writeASTVisitorTraversal(raw_ostream &OS) const override { | 
|  | 1227 | OS << "  if (auto *TSI = A->get" << getUpperName() << "Loc())\n"; | 
|  | 1228 | OS << "    if (!getDerived().TraverseTypeLoc(TSI->getTypeLoc()))\n"; | 
|  | 1229 | OS << "      return false;\n"; | 
|  | 1230 | } | 
|  | 1231 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1232 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { | 
| Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1233 | OS << "A->get" << getUpperName() << "Loc()"; | 
|  | 1234 | } | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1235 |  | 
| Aaron Ballman | 8cbf633 | 2014-03-06 15:09:50 +0000 | [diff] [blame] | 1236 | void writePCHWrite(raw_ostream &OS) const override { | 
| Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1237 | OS << "    " << WritePCHRecord( | 
|  | 1238 | getType(), "SA->get" + std::string(getUpperName()) + "Loc()"); | 
|  | 1239 | } | 
|  | 1240 | }; | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1241 |  | 
| Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1242 | } // end anonymous namespace | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1243 |  | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 1244 | static std::unique_ptr<Argument> | 
|  | 1245 | createArgument(const Record &Arg, StringRef Attr, | 
|  | 1246 | const Record *Search = nullptr) { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1247 | if (!Search) | 
|  | 1248 | Search = &Arg; | 
|  | 1249 |  | 
| David Blaikie | 28f30ca | 2014-08-08 23:59:38 +0000 | [diff] [blame] | 1250 | std::unique_ptr<Argument> Ptr; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1251 | llvm::StringRef ArgName = Search->getName(); | 
|  | 1252 |  | 
| David Blaikie | 28f30ca | 2014-08-08 23:59:38 +0000 | [diff] [blame] | 1253 | if (ArgName == "AlignedArgument") | 
|  | 1254 | Ptr = llvm::make_unique<AlignedArgument>(Arg, Attr); | 
|  | 1255 | else if (ArgName == "EnumArgument") | 
|  | 1256 | Ptr = llvm::make_unique<EnumArgument>(Arg, Attr); | 
|  | 1257 | else if (ArgName == "ExprArgument") | 
|  | 1258 | Ptr = llvm::make_unique<ExprArgument>(Arg, Attr); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1259 | else if (ArgName == "FunctionArgument") | 
| David Blaikie | 28f30ca | 2014-08-08 23:59:38 +0000 | [diff] [blame] | 1260 | Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "FunctionDecl *"); | 
| Argyrios Kyrtzidis | a7233bd | 2017-05-24 00:46:27 +0000 | [diff] [blame] | 1261 | else if (ArgName == "NamedArgument") | 
|  | 1262 | Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "NamedDecl *"); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1263 | else if (ArgName == "IdentifierArgument") | 
| David Blaikie | 28f30ca | 2014-08-08 23:59:38 +0000 | [diff] [blame] | 1264 | Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "IdentifierInfo *"); | 
| David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 1265 | else if (ArgName == "DefaultBoolArgument") | 
| David Blaikie | 28f30ca | 2014-08-08 23:59:38 +0000 | [diff] [blame] | 1266 | Ptr = llvm::make_unique<DefaultSimpleArgument>( | 
|  | 1267 | Arg, Attr, "bool", Arg.getValueAsBit("Default")); | 
|  | 1268 | else if (ArgName == "BoolArgument") | 
|  | 1269 | Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "bool"); | 
| Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 1270 | else if (ArgName == "DefaultIntArgument") | 
| David Blaikie | 28f30ca | 2014-08-08 23:59:38 +0000 | [diff] [blame] | 1271 | Ptr = llvm::make_unique<DefaultSimpleArgument>( | 
|  | 1272 | Arg, Attr, "int", Arg.getValueAsInt("Default")); | 
|  | 1273 | else if (ArgName == "IntArgument") | 
|  | 1274 | Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "int"); | 
|  | 1275 | else if (ArgName == "StringArgument") | 
|  | 1276 | Ptr = llvm::make_unique<StringArgument>(Arg, Attr); | 
|  | 1277 | else if (ArgName == "TypeArgument") | 
|  | 1278 | Ptr = llvm::make_unique<TypeArgument>(Arg, Attr); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1279 | else if (ArgName == "UnsignedArgument") | 
| David Blaikie | 28f30ca | 2014-08-08 23:59:38 +0000 | [diff] [blame] | 1280 | Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "unsigned"); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1281 | else if (ArgName == "VariadicUnsignedArgument") | 
| David Blaikie | 28f30ca | 2014-08-08 23:59:38 +0000 | [diff] [blame] | 1282 | Ptr = llvm::make_unique<VariadicArgument>(Arg, Attr, "unsigned"); | 
| Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 1283 | else if (ArgName == "VariadicStringArgument") | 
|  | 1284 | Ptr = llvm::make_unique<VariadicStringArgument>(Arg, Attr); | 
| DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1285 | else if (ArgName == "VariadicEnumArgument") | 
| David Blaikie | 28f30ca | 2014-08-08 23:59:38 +0000 | [diff] [blame] | 1286 | Ptr = llvm::make_unique<VariadicEnumArgument>(Arg, Attr); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1287 | else if (ArgName == "VariadicExprArgument") | 
| David Blaikie | 28f30ca | 2014-08-08 23:59:38 +0000 | [diff] [blame] | 1288 | Ptr = llvm::make_unique<VariadicExprArgument>(Arg, Attr); | 
| Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1289 | else if (ArgName == "VariadicParamIdxArgument") | 
|  | 1290 | Ptr = llvm::make_unique<VariadicParamIdxArgument>(Arg, Attr); | 
| Johannes Doerfert | ac991bb | 2019-01-19 05:36:54 +0000 | [diff] [blame] | 1291 | else if (ArgName == "VariadicParamOrParamIdxArgument") | 
|  | 1292 | Ptr = llvm::make_unique<VariadicParamOrParamIdxArgument>(Arg, Attr); | 
| Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 1293 | else if (ArgName == "ParamIdxArgument") | 
|  | 1294 | Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "ParamIdx"); | 
| Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 1295 | else if (ArgName == "VariadicIdentifierArgument") | 
|  | 1296 | Ptr = llvm::make_unique<VariadicIdentifierArgument>(Arg, Attr); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1297 | else if (ArgName == "VersionArgument") | 
| David Blaikie | 28f30ca | 2014-08-08 23:59:38 +0000 | [diff] [blame] | 1298 | Ptr = llvm::make_unique<VersionArgument>(Arg, Attr); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1299 |  | 
|  | 1300 | if (!Ptr) { | 
| Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 1301 | // Search in reverse order so that the most-derived type is handled first. | 
| Craig Topper | 2576124 | 2016-01-18 19:52:54 +0000 | [diff] [blame] | 1302 | ArrayRef<std::pair<Record*, SMRange>> Bases = Search->getSuperClasses(); | 
| David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 1303 | for (const auto &Base : llvm::reverse(Bases)) { | 
| Craig Topper | 2576124 | 2016-01-18 19:52:54 +0000 | [diff] [blame] | 1304 | if ((Ptr = createArgument(Arg, Attr, Base.first))) | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1305 | break; | 
|  | 1306 | } | 
|  | 1307 | } | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 1308 |  | 
|  | 1309 | if (Ptr && Arg.getValueAsBit("Optional")) | 
|  | 1310 | Ptr->setOptional(true); | 
|  | 1311 |  | 
| John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 1312 | if (Ptr && Arg.getValueAsBit("Fake")) | 
|  | 1313 | Ptr->setFake(true); | 
|  | 1314 |  | 
| David Blaikie | 28f30ca | 2014-08-08 23:59:38 +0000 | [diff] [blame] | 1315 | return Ptr; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1316 | } | 
|  | 1317 |  | 
| Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 1318 | static void writeAvailabilityValue(raw_ostream &OS) { | 
|  | 1319 | OS << "\" << getPlatform()->getName();\n" | 
| Manman Ren | 42e09eb | 2016-03-10 23:54:12 +0000 | [diff] [blame] | 1320 | << "  if (getStrict()) OS << \", strict\";\n" | 
| Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 1321 | << "  if (!getIntroduced().empty()) OS << \", introduced=\" << getIntroduced();\n" | 
|  | 1322 | << "  if (!getDeprecated().empty()) OS << \", deprecated=\" << getDeprecated();\n" | 
|  | 1323 | << "  if (!getObsoleted().empty()) OS << \", obsoleted=\" << getObsoleted();\n" | 
|  | 1324 | << "  if (getUnavailable()) OS << \", unavailable\";\n" | 
|  | 1325 | << "  OS << \""; | 
|  | 1326 | } | 
|  | 1327 |  | 
| Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 1328 | static void writeDeprecatedAttrValue(raw_ostream &OS, std::string &Variety) { | 
|  | 1329 | OS << "\\\"\" << getMessage() << \"\\\"\";\n"; | 
|  | 1330 | // Only GNU deprecated has an optional fixit argument at the second position. | 
|  | 1331 | if (Variety == "GNU") | 
|  | 1332 | OS << "    if (!getReplacement().empty()) OS << \", \\\"\"" | 
|  | 1333 | " << getReplacement() << \"\\\"\";\n"; | 
|  | 1334 | OS << "    OS << \""; | 
|  | 1335 | } | 
|  | 1336 |  | 
| Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 1337 | static void writeGetSpellingFunction(Record &R, raw_ostream &OS) { | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1338 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R); | 
| Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 1339 |  | 
|  | 1340 | OS << "const char *" << R.getName() << "Attr::getSpelling() const {\n"; | 
|  | 1341 | if (Spellings.empty()) { | 
|  | 1342 | OS << "  return \"(No spelling)\";\n}\n\n"; | 
|  | 1343 | return; | 
|  | 1344 | } | 
|  | 1345 |  | 
|  | 1346 | OS << "  switch (SpellingListIndex) {\n" | 
|  | 1347 | "  default:\n" | 
|  | 1348 | "    llvm_unreachable(\"Unknown attribute spelling!\");\n" | 
|  | 1349 | "    return \"(No spelling)\";\n"; | 
|  | 1350 |  | 
|  | 1351 | for (unsigned I = 0; I < Spellings.size(); ++I) | 
|  | 1352 | OS << "  case " << I << ":\n" | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1353 | "    return \"" << Spellings[I].name() << "\";\n"; | 
| Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 1354 | // End of the switch statement. | 
|  | 1355 | OS << "  }\n"; | 
|  | 1356 | // End of the getSpelling function. | 
|  | 1357 | OS << "}\n\n"; | 
|  | 1358 | } | 
|  | 1359 |  | 
| Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 1360 | static void | 
|  | 1361 | writePrettyPrintFunction(Record &R, | 
|  | 1362 | const std::vector<std::unique_ptr<Argument>> &Args, | 
|  | 1363 | raw_ostream &OS) { | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1364 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R); | 
| Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1365 |  | 
|  | 1366 | OS << "void " << R.getName() << "Attr::printPretty(" | 
|  | 1367 | << "raw_ostream &OS, const PrintingPolicy &Policy) const {\n"; | 
|  | 1368 |  | 
| Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 1369 | if (Spellings.empty()) { | 
| Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1370 | OS << "}\n\n"; | 
|  | 1371 | return; | 
|  | 1372 | } | 
|  | 1373 |  | 
|  | 1374 | OS << | 
|  | 1375 | "  switch (SpellingListIndex) {\n" | 
|  | 1376 | "  default:\n" | 
|  | 1377 | "    llvm_unreachable(\"Unknown attribute spelling!\");\n" | 
|  | 1378 | "    break;\n"; | 
|  | 1379 |  | 
|  | 1380 | for (unsigned I = 0; I < Spellings.size(); ++ I) { | 
|  | 1381 | llvm::SmallString<16> Prefix; | 
|  | 1382 | llvm::SmallString<8> Suffix; | 
|  | 1383 | // The actual spelling of the name and namespace (if applicable) | 
|  | 1384 | // of an attribute without considering prefix and suffix. | 
|  | 1385 | llvm::SmallString<64> Spelling; | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1386 | std::string Name = Spellings[I].name(); | 
|  | 1387 | std::string Variety = Spellings[I].variety(); | 
| Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1388 |  | 
|  | 1389 | if (Variety == "GNU") { | 
|  | 1390 | Prefix = " __attribute__(("; | 
|  | 1391 | Suffix = "))"; | 
| Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 1392 | } else if (Variety == "CXX11" || Variety == "C2x") { | 
| Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1393 | Prefix = " [["; | 
|  | 1394 | Suffix = "]]"; | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1395 | std::string Namespace = Spellings[I].nameSpace(); | 
| Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 1396 | if (!Namespace.empty()) { | 
| Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1397 | Spelling += Namespace; | 
|  | 1398 | Spelling += "::"; | 
|  | 1399 | } | 
|  | 1400 | } else if (Variety == "Declspec") { | 
|  | 1401 | Prefix = " __declspec("; | 
|  | 1402 | Suffix = ")"; | 
| Nico Weber | 20e0804 | 2016-09-03 02:55:10 +0000 | [diff] [blame] | 1403 | } else if (Variety == "Microsoft") { | 
|  | 1404 | Prefix = "["; | 
|  | 1405 | Suffix = "]"; | 
| Richard Smith | 0cdcc98 | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 1406 | } else if (Variety == "Keyword") { | 
|  | 1407 | Prefix = " "; | 
|  | 1408 | Suffix = ""; | 
| Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 1409 | } else if (Variety == "Pragma") { | 
|  | 1410 | Prefix = "#pragma "; | 
|  | 1411 | Suffix = "\n"; | 
|  | 1412 | std::string Namespace = Spellings[I].nameSpace(); | 
|  | 1413 | if (!Namespace.empty()) { | 
|  | 1414 | Spelling += Namespace; | 
|  | 1415 | Spelling += " "; | 
|  | 1416 | } | 
| Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1417 | } else { | 
| Richard Smith | 0cdcc98 | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 1418 | llvm_unreachable("Unknown attribute syntax variety!"); | 
| Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1419 | } | 
|  | 1420 |  | 
|  | 1421 | Spelling += Name; | 
|  | 1422 |  | 
|  | 1423 | OS << | 
|  | 1424 | "  case " << I << " : {\n" | 
| Yaron Keren | 09fb7c6 | 2015-03-10 07:33:23 +0000 | [diff] [blame] | 1425 | "    OS << \"" << Prefix << Spelling; | 
| Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1426 |  | 
| Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 1427 | if (Variety == "Pragma") { | 
| Alexey Bataev | cbecfdf | 2018-02-14 17:38:47 +0000 | [diff] [blame] | 1428 | OS << "\";\n"; | 
| Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 1429 | OS << "    printPrettyPragma(OS, Policy);\n"; | 
| Alexey Bataev | 6d45532 | 2015-10-12 06:59:48 +0000 | [diff] [blame] | 1430 | OS << "    OS << \"\\n\";"; | 
| Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 1431 | OS << "    break;\n"; | 
|  | 1432 | OS << "  }\n"; | 
|  | 1433 | continue; | 
|  | 1434 | } | 
|  | 1435 |  | 
| Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1436 | if (Spelling == "availability") { | 
| Aaron Ballman | 48a533d | 2018-02-27 23:49:28 +0000 | [diff] [blame] | 1437 | OS << "("; | 
| Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1438 | writeAvailabilityValue(OS); | 
| Aaron Ballman | 48a533d | 2018-02-27 23:49:28 +0000 | [diff] [blame] | 1439 | OS << ")"; | 
| Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 1440 | } else if (Spelling == "deprecated" || Spelling == "gnu::deprecated") { | 
| Aaron Ballman | 48a533d | 2018-02-27 23:49:28 +0000 | [diff] [blame] | 1441 | OS << "("; | 
|  | 1442 | writeDeprecatedAttrValue(OS, Variety); | 
|  | 1443 | OS << ")"; | 
| Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1444 | } else { | 
| Aaron Ballman | 48a533d | 2018-02-27 23:49:28 +0000 | [diff] [blame] | 1445 | // To avoid printing parentheses around an empty argument list or | 
|  | 1446 | // printing spurious commas at the end of an argument list, we need to | 
|  | 1447 | // determine where the last provided non-fake argument is. | 
|  | 1448 | unsigned NonFakeArgs = 0; | 
|  | 1449 | unsigned TrailingOptArgs = 0; | 
|  | 1450 | bool FoundNonOptArg = false; | 
|  | 1451 | for (const auto &arg : llvm::reverse(Args)) { | 
|  | 1452 | if (arg->isFake()) | 
|  | 1453 | continue; | 
|  | 1454 | ++NonFakeArgs; | 
|  | 1455 | if (FoundNonOptArg) | 
|  | 1456 | continue; | 
|  | 1457 | // FIXME: arg->getIsOmitted() == "false" means we haven't implemented | 
|  | 1458 | // any way to detect whether the argument was omitted. | 
|  | 1459 | if (!arg->isOptional() || arg->getIsOmitted() == "false") { | 
|  | 1460 | FoundNonOptArg = true; | 
|  | 1461 | continue; | 
|  | 1462 | } | 
|  | 1463 | if (!TrailingOptArgs++) | 
|  | 1464 | OS << "\";\n" | 
|  | 1465 | << "    unsigned TrailingOmittedArgs = 0;\n"; | 
|  | 1466 | OS << "    if (" << arg->getIsOmitted() << ")\n" | 
|  | 1467 | << "      ++TrailingOmittedArgs;\n"; | 
| Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1468 | } | 
| Aaron Ballman | 48a533d | 2018-02-27 23:49:28 +0000 | [diff] [blame] | 1469 | if (TrailingOptArgs) | 
|  | 1470 | OS << "    OS << \""; | 
|  | 1471 | if (TrailingOptArgs < NonFakeArgs) | 
|  | 1472 | OS << "("; | 
|  | 1473 | else if (TrailingOptArgs) | 
|  | 1474 | OS << "\";\n" | 
|  | 1475 | << "    if (TrailingOmittedArgs < " << NonFakeArgs << ")\n" | 
|  | 1476 | << "       OS << \"(\";\n" | 
|  | 1477 | << "    OS << \""; | 
|  | 1478 | unsigned ArgIndex = 0; | 
|  | 1479 | for (const auto &arg : Args) { | 
|  | 1480 | if (arg->isFake()) | 
|  | 1481 | continue; | 
|  | 1482 | if (ArgIndex) { | 
|  | 1483 | if (ArgIndex >= NonFakeArgs - TrailingOptArgs) | 
|  | 1484 | OS << "\";\n" | 
|  | 1485 | << "    if (" << ArgIndex << " < " << NonFakeArgs | 
|  | 1486 | << " - TrailingOmittedArgs)\n" | 
|  | 1487 | << "      OS << \", \";\n" | 
|  | 1488 | << "    OS << \""; | 
|  | 1489 | else | 
|  | 1490 | OS << ", "; | 
|  | 1491 | } | 
|  | 1492 | std::string IsOmitted = arg->getIsOmitted(); | 
|  | 1493 | if (arg->isOptional() && IsOmitted != "false") | 
|  | 1494 | OS << "\";\n" | 
|  | 1495 | << "    if (!(" << IsOmitted << ")) {\n" | 
|  | 1496 | << "      OS << \""; | 
|  | 1497 | arg->writeValue(OS); | 
|  | 1498 | if (arg->isOptional() && IsOmitted != "false") | 
|  | 1499 | OS << "\";\n" | 
|  | 1500 | << "    }\n" | 
|  | 1501 | << "    OS << \""; | 
|  | 1502 | ++ArgIndex; | 
|  | 1503 | } | 
|  | 1504 | if (TrailingOptArgs < NonFakeArgs) | 
|  | 1505 | OS << ")"; | 
|  | 1506 | else if (TrailingOptArgs) | 
|  | 1507 | OS << "\";\n" | 
|  | 1508 | << "    if (TrailingOmittedArgs < " << NonFakeArgs << ")\n" | 
|  | 1509 | << "       OS << \")\";\n" | 
|  | 1510 | << "    OS << \""; | 
| Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1511 | } | 
|  | 1512 |  | 
| Yaron Keren | 09fb7c6 | 2015-03-10 07:33:23 +0000 | [diff] [blame] | 1513 | OS << Suffix + "\";\n"; | 
| Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1514 |  | 
|  | 1515 | OS << | 
|  | 1516 | "    break;\n" | 
|  | 1517 | "  }\n"; | 
|  | 1518 | } | 
|  | 1519 |  | 
|  | 1520 | // End of the switch statement. | 
|  | 1521 | OS << "}\n"; | 
|  | 1522 | // End of the print function. | 
|  | 1523 | OS << "}\n\n"; | 
|  | 1524 | } | 
|  | 1525 |  | 
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1526 | /// Return the index of a spelling in a spelling list. | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1527 | static unsigned | 
|  | 1528 | getSpellingListIndex(const std::vector<FlattenedSpelling> &SpellingList, | 
|  | 1529 | const FlattenedSpelling &Spelling) { | 
| Alexander Kornienko | 6ee521c | 2015-01-23 15:36:10 +0000 | [diff] [blame] | 1530 | assert(!SpellingList.empty() && "Spelling list is empty!"); | 
| Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 1531 |  | 
|  | 1532 | for (unsigned Index = 0; Index < SpellingList.size(); ++Index) { | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1533 | const FlattenedSpelling &S = SpellingList[Index]; | 
|  | 1534 | if (S.variety() != Spelling.variety()) | 
| Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 1535 | continue; | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1536 | if (S.nameSpace() != Spelling.nameSpace()) | 
| Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 1537 | continue; | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1538 | if (S.name() != Spelling.name()) | 
| Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 1539 | continue; | 
|  | 1540 |  | 
|  | 1541 | return Index; | 
|  | 1542 | } | 
|  | 1543 |  | 
|  | 1544 | llvm_unreachable("Unknown spelling!"); | 
|  | 1545 | } | 
|  | 1546 |  | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 1547 | static void writeAttrAccessorDefinition(const Record &R, raw_ostream &OS) { | 
| Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 1548 | std::vector<Record*> Accessors = R.getValueAsListOfDefs("Accessors"); | 
| George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 1549 | if (Accessors.empty()) | 
|  | 1550 | return; | 
|  | 1551 |  | 
|  | 1552 | const std::vector<FlattenedSpelling> SpellingList = GetFlattenedSpellings(R); | 
|  | 1553 | assert(!SpellingList.empty() && | 
|  | 1554 | "Attribute with empty spelling list can't have accessors!"); | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 1555 | for (const auto *Accessor : Accessors) { | 
| Erich Keane | 3bff414 | 2017-10-16 23:25:24 +0000 | [diff] [blame] | 1556 | const StringRef Name = Accessor->getValueAsString("Name"); | 
| George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 1557 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Accessor); | 
| Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 1558 |  | 
|  | 1559 | OS << "  bool " << Name << "() const { return SpellingListIndex == "; | 
|  | 1560 | for (unsigned Index = 0; Index < Spellings.size(); ++Index) { | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1561 | OS << getSpellingListIndex(SpellingList, Spellings[Index]); | 
| George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 1562 | if (Index != Spellings.size() - 1) | 
| Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 1563 | OS << " ||\n    SpellingListIndex == "; | 
|  | 1564 | else | 
|  | 1565 | OS << "; }\n"; | 
|  | 1566 | } | 
|  | 1567 | } | 
|  | 1568 | } | 
|  | 1569 |  | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1570 | static bool | 
|  | 1571 | SpellingNamesAreCommon(const std::vector<FlattenedSpelling>& Spellings) { | 
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 1572 | assert(!Spellings.empty() && "An empty list of spellings was provided"); | 
|  | 1573 | std::string FirstName = NormalizeNameForSpellingComparison( | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1574 | Spellings.front().name()); | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 1575 | for (const auto &Spelling : | 
|  | 1576 | llvm::make_range(std::next(Spellings.begin()), Spellings.end())) { | 
|  | 1577 | std::string Name = NormalizeNameForSpellingComparison(Spelling.name()); | 
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 1578 | if (Name != FirstName) | 
|  | 1579 | return false; | 
|  | 1580 | } | 
|  | 1581 | return true; | 
|  | 1582 | } | 
|  | 1583 |  | 
| Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 1584 | typedef std::map<unsigned, std::string> SemanticSpellingMap; | 
|  | 1585 | static std::string | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1586 | CreateSemanticSpellings(const std::vector<FlattenedSpelling> &Spellings, | 
| Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 1587 | SemanticSpellingMap &Map) { | 
|  | 1588 | // The enumerants are automatically generated based on the variety, | 
|  | 1589 | // namespace (if present) and name for each attribute spelling. However, | 
|  | 1590 | // care is taken to avoid trampling on the reserved namespace due to | 
|  | 1591 | // underscores. | 
|  | 1592 | std::string Ret("  enum Spelling {\n"); | 
|  | 1593 | std::set<std::string> Uniques; | 
|  | 1594 | unsigned Idx = 0; | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 1595 | for (auto I = Spellings.begin(), E = Spellings.end(); I != E; ++I, ++Idx) { | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1596 | const FlattenedSpelling &S = *I; | 
| Benjamin Kramer | 2e018ef | 2016-05-27 13:36:58 +0000 | [diff] [blame] | 1597 | const std::string &Variety = S.variety(); | 
|  | 1598 | const std::string &Spelling = S.name(); | 
|  | 1599 | const std::string &Namespace = S.nameSpace(); | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 1600 | std::string EnumName; | 
| Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 1601 |  | 
| Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 1602 | EnumName += (Variety + "_"); | 
|  | 1603 | if (!Namespace.empty()) | 
|  | 1604 | EnumName += (NormalizeNameForSpellingComparison(Namespace).str() + | 
|  | 1605 | "_"); | 
|  | 1606 | EnumName += NormalizeNameForSpellingComparison(Spelling); | 
|  | 1607 |  | 
|  | 1608 | // Even if the name is not unique, this spelling index corresponds to a | 
|  | 1609 | // particular enumerant name that we've calculated. | 
|  | 1610 | Map[Idx] = EnumName; | 
|  | 1611 |  | 
|  | 1612 | // Since we have been stripping underscores to avoid trampling on the | 
|  | 1613 | // reserved namespace, we may have inadvertently created duplicate | 
|  | 1614 | // enumerant names. These duplicates are not considered part of the | 
|  | 1615 | // semantic spelling, and can be elided. | 
|  | 1616 | if (Uniques.find(EnumName) != Uniques.end()) | 
|  | 1617 | continue; | 
|  | 1618 |  | 
|  | 1619 | Uniques.insert(EnumName); | 
|  | 1620 | if (I != Spellings.begin()) | 
|  | 1621 | Ret += ",\n"; | 
| Aaron Ballman | 9bf6b75 | 2015-03-10 17:19:18 +0000 | [diff] [blame] | 1622 | // Duplicate spellings are not considered part of the semantic spelling | 
|  | 1623 | // enumeration, but the spelling index and semantic spelling values are | 
|  | 1624 | // meant to be equivalent, so we must specify a concrete value for each | 
|  | 1625 | // enumerator. | 
|  | 1626 | Ret += "    " + EnumName + " = " + llvm::utostr(Idx); | 
| Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 1627 | } | 
|  | 1628 | Ret += "\n  };\n\n"; | 
|  | 1629 | return Ret; | 
|  | 1630 | } | 
|  | 1631 |  | 
|  | 1632 | void WriteSemanticSpellingSwitch(const std::string &VarName, | 
|  | 1633 | const SemanticSpellingMap &Map, | 
|  | 1634 | raw_ostream &OS) { | 
|  | 1635 | OS << "  switch (" << VarName << ") {\n    default: " | 
|  | 1636 | << "llvm_unreachable(\"Unknown spelling list index\");\n"; | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 1637 | for (const auto &I : Map) | 
|  | 1638 | OS << "    case " << I.first << ": return " << I.second << ";\n"; | 
| Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 1639 | OS << "  }\n"; | 
|  | 1640 | } | 
|  | 1641 |  | 
| Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 1642 | // Emits the LateParsed property for attributes. | 
|  | 1643 | static void emitClangAttrLateParsedList(RecordKeeper &Records, raw_ostream &OS) { | 
|  | 1644 | OS << "#if defined(CLANG_ATTR_LATE_PARSED_LIST)\n"; | 
|  | 1645 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"); | 
|  | 1646 |  | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 1647 | for (const auto *Attr : Attrs) { | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 1648 | bool LateParsed = Attr->getValueAsBit("LateParsed"); | 
| Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 1649 |  | 
|  | 1650 | if (LateParsed) { | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 1651 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr); | 
| Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 1652 |  | 
|  | 1653 | // FIXME: Handle non-GNU attributes | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 1654 | for (const auto &I : Spellings) { | 
|  | 1655 | if (I.variety() != "GNU") | 
| Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 1656 | continue; | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 1657 | OS << ".Case(\"" << I.name() << "\", " << LateParsed << ")\n"; | 
| Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 1658 | } | 
|  | 1659 | } | 
|  | 1660 | } | 
|  | 1661 | OS << "#endif // CLANG_ATTR_LATE_PARSED_LIST\n\n"; | 
|  | 1662 | } | 
|  | 1663 |  | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1664 | static bool hasGNUorCXX11Spelling(const Record &Attribute) { | 
|  | 1665 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attribute); | 
|  | 1666 | for (const auto &I : Spellings) { | 
|  | 1667 | if (I.variety() == "GNU" || I.variety() == "CXX11") | 
|  | 1668 | return true; | 
|  | 1669 | } | 
|  | 1670 | return false; | 
|  | 1671 | } | 
|  | 1672 |  | 
|  | 1673 | namespace { | 
|  | 1674 |  | 
|  | 1675 | struct AttributeSubjectMatchRule { | 
|  | 1676 | const Record *MetaSubject; | 
|  | 1677 | const Record *Constraint; | 
|  | 1678 |  | 
|  | 1679 | AttributeSubjectMatchRule(const Record *MetaSubject, const Record *Constraint) | 
|  | 1680 | : MetaSubject(MetaSubject), Constraint(Constraint) { | 
|  | 1681 | assert(MetaSubject && "Missing subject"); | 
|  | 1682 | } | 
|  | 1683 |  | 
|  | 1684 | bool isSubRule() const { return Constraint != nullptr; } | 
|  | 1685 |  | 
|  | 1686 | std::vector<Record *> getSubjects() const { | 
|  | 1687 | return (Constraint ? Constraint : MetaSubject) | 
|  | 1688 | ->getValueAsListOfDefs("Subjects"); | 
|  | 1689 | } | 
|  | 1690 |  | 
|  | 1691 | std::vector<Record *> getLangOpts() const { | 
|  | 1692 | if (Constraint) { | 
|  | 1693 | // Lookup the options in the sub-rule first, in case the sub-rule | 
|  | 1694 | // overrides the rules options. | 
|  | 1695 | std::vector<Record *> Opts = Constraint->getValueAsListOfDefs("LangOpts"); | 
|  | 1696 | if (!Opts.empty()) | 
|  | 1697 | return Opts; | 
|  | 1698 | } | 
|  | 1699 | return MetaSubject->getValueAsListOfDefs("LangOpts"); | 
|  | 1700 | } | 
|  | 1701 |  | 
|  | 1702 | // Abstract rules are used only for sub-rules | 
|  | 1703 | bool isAbstractRule() const { return getSubjects().empty(); } | 
|  | 1704 |  | 
| Erich Keane | 3bff414 | 2017-10-16 23:25:24 +0000 | [diff] [blame] | 1705 | StringRef getName() const { | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1706 | return (Constraint ? Constraint : MetaSubject)->getValueAsString("Name"); | 
|  | 1707 | } | 
|  | 1708 |  | 
|  | 1709 | bool isNegatedSubRule() const { | 
|  | 1710 | assert(isSubRule() && "Not a sub-rule"); | 
|  | 1711 | return Constraint->getValueAsBit("Negated"); | 
|  | 1712 | } | 
|  | 1713 |  | 
|  | 1714 | std::string getSpelling() const { | 
|  | 1715 | std::string Result = MetaSubject->getValueAsString("Name"); | 
|  | 1716 | if (isSubRule()) { | 
|  | 1717 | Result += '('; | 
|  | 1718 | if (isNegatedSubRule()) | 
|  | 1719 | Result += "unless("; | 
|  | 1720 | Result += getName(); | 
|  | 1721 | if (isNegatedSubRule()) | 
|  | 1722 | Result += ')'; | 
|  | 1723 | Result += ')'; | 
|  | 1724 | } | 
|  | 1725 | return Result; | 
|  | 1726 | } | 
|  | 1727 |  | 
|  | 1728 | std::string getEnumValueName() const { | 
| Craig Topper | 0064858 | 2017-05-31 19:01:22 +0000 | [diff] [blame] | 1729 | SmallString<128> Result; | 
|  | 1730 | Result += "SubjectMatchRule_"; | 
|  | 1731 | Result += MetaSubject->getValueAsString("Name"); | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1732 | if (isSubRule()) { | 
|  | 1733 | Result += "_"; | 
|  | 1734 | if (isNegatedSubRule()) | 
|  | 1735 | Result += "not_"; | 
|  | 1736 | Result += Constraint->getValueAsString("Name"); | 
|  | 1737 | } | 
|  | 1738 | if (isAbstractRule()) | 
|  | 1739 | Result += "_abstract"; | 
| Craig Topper | 0064858 | 2017-05-31 19:01:22 +0000 | [diff] [blame] | 1740 | return Result.str(); | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1741 | } | 
|  | 1742 |  | 
|  | 1743 | std::string getEnumValue() const { return "attr::" + getEnumValueName(); } | 
|  | 1744 |  | 
|  | 1745 | static const char *EnumName; | 
|  | 1746 | }; | 
|  | 1747 |  | 
|  | 1748 | const char *AttributeSubjectMatchRule::EnumName = "attr::SubjectMatchRule"; | 
|  | 1749 |  | 
|  | 1750 | struct PragmaClangAttributeSupport { | 
|  | 1751 | std::vector<AttributeSubjectMatchRule> Rules; | 
| Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 1752 |  | 
|  | 1753 | class RuleOrAggregateRuleSet { | 
|  | 1754 | std::vector<AttributeSubjectMatchRule> Rules; | 
|  | 1755 | bool IsRule; | 
|  | 1756 | RuleOrAggregateRuleSet(ArrayRef<AttributeSubjectMatchRule> Rules, | 
|  | 1757 | bool IsRule) | 
|  | 1758 | : Rules(Rules), IsRule(IsRule) {} | 
|  | 1759 |  | 
|  | 1760 | public: | 
|  | 1761 | bool isRule() const { return IsRule; } | 
|  | 1762 |  | 
|  | 1763 | const AttributeSubjectMatchRule &getRule() const { | 
|  | 1764 | assert(IsRule && "not a rule!"); | 
|  | 1765 | return Rules[0]; | 
|  | 1766 | } | 
|  | 1767 |  | 
|  | 1768 | ArrayRef<AttributeSubjectMatchRule> getAggregateRuleSet() const { | 
|  | 1769 | return Rules; | 
|  | 1770 | } | 
|  | 1771 |  | 
|  | 1772 | static RuleOrAggregateRuleSet | 
|  | 1773 | getRule(const AttributeSubjectMatchRule &Rule) { | 
|  | 1774 | return RuleOrAggregateRuleSet(Rule, /*IsRule=*/true); | 
|  | 1775 | } | 
|  | 1776 | static RuleOrAggregateRuleSet | 
|  | 1777 | getAggregateRuleSet(ArrayRef<AttributeSubjectMatchRule> Rules) { | 
|  | 1778 | return RuleOrAggregateRuleSet(Rules, /*IsRule=*/false); | 
|  | 1779 | } | 
|  | 1780 | }; | 
|  | 1781 | llvm::DenseMap<const Record *, RuleOrAggregateRuleSet> SubjectsToRules; | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1782 |  | 
|  | 1783 | PragmaClangAttributeSupport(RecordKeeper &Records); | 
|  | 1784 |  | 
|  | 1785 | bool isAttributedSupported(const Record &Attribute); | 
|  | 1786 |  | 
|  | 1787 | void emitMatchRuleList(raw_ostream &OS); | 
|  | 1788 |  | 
|  | 1789 | std::string generateStrictConformsTo(const Record &Attr, raw_ostream &OS); | 
|  | 1790 |  | 
|  | 1791 | void generateParsingHelpers(raw_ostream &OS); | 
|  | 1792 | }; | 
|  | 1793 |  | 
|  | 1794 | } // end anonymous namespace | 
|  | 1795 |  | 
| Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 1796 | static bool doesDeclDeriveFrom(const Record *D, const Record *Base) { | 
|  | 1797 | const Record *CurrentBase = D->getValueAsDef("Base"); | 
|  | 1798 | if (!CurrentBase) | 
|  | 1799 | return false; | 
|  | 1800 | if (CurrentBase == Base) | 
|  | 1801 | return true; | 
|  | 1802 | return doesDeclDeriveFrom(CurrentBase, Base); | 
|  | 1803 | } | 
|  | 1804 |  | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1805 | PragmaClangAttributeSupport::PragmaClangAttributeSupport( | 
|  | 1806 | RecordKeeper &Records) { | 
|  | 1807 | std::vector<Record *> MetaSubjects = | 
|  | 1808 | Records.getAllDerivedDefinitions("AttrSubjectMatcherRule"); | 
|  | 1809 | auto MapFromSubjectsToRules = [this](const Record *SubjectContainer, | 
|  | 1810 | const Record *MetaSubject, | 
| Saleem Abdulrasool | be4773c | 2017-05-01 00:26:59 +0000 | [diff] [blame] | 1811 | const Record *Constraint) { | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1812 | Rules.emplace_back(MetaSubject, Constraint); | 
|  | 1813 | std::vector<Record *> ApplicableSubjects = | 
|  | 1814 | SubjectContainer->getValueAsListOfDefs("Subjects"); | 
|  | 1815 | for (const auto *Subject : ApplicableSubjects) { | 
|  | 1816 | bool Inserted = | 
| Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 1817 | SubjectsToRules | 
|  | 1818 | .try_emplace(Subject, RuleOrAggregateRuleSet::getRule( | 
|  | 1819 | AttributeSubjectMatchRule(MetaSubject, | 
|  | 1820 | Constraint))) | 
|  | 1821 | .second; | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1822 | if (!Inserted) { | 
|  | 1823 | PrintFatalError("Attribute subject match rules should not represent" | 
|  | 1824 | "same attribute subjects."); | 
|  | 1825 | } | 
|  | 1826 | } | 
|  | 1827 | }; | 
|  | 1828 | for (const auto *MetaSubject : MetaSubjects) { | 
| Saleem Abdulrasool | be4773c | 2017-05-01 00:26:59 +0000 | [diff] [blame] | 1829 | MapFromSubjectsToRules(MetaSubject, MetaSubject, /*Constraints=*/nullptr); | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1830 | std::vector<Record *> Constraints = | 
|  | 1831 | MetaSubject->getValueAsListOfDefs("Constraints"); | 
|  | 1832 | for (const auto *Constraint : Constraints) | 
|  | 1833 | MapFromSubjectsToRules(Constraint, MetaSubject, Constraint); | 
|  | 1834 | } | 
| Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 1835 |  | 
|  | 1836 | std::vector<Record *> Aggregates = | 
|  | 1837 | Records.getAllDerivedDefinitions("AttrSubjectMatcherAggregateRule"); | 
|  | 1838 | std::vector<Record *> DeclNodes = Records.getAllDerivedDefinitions("DDecl"); | 
|  | 1839 | for (const auto *Aggregate : Aggregates) { | 
|  | 1840 | Record *SubjectDecl = Aggregate->getValueAsDef("Subject"); | 
|  | 1841 |  | 
|  | 1842 | // Gather sub-classes of the aggregate subject that act as attribute | 
|  | 1843 | // subject rules. | 
|  | 1844 | std::vector<AttributeSubjectMatchRule> Rules; | 
|  | 1845 | for (const auto *D : DeclNodes) { | 
|  | 1846 | if (doesDeclDeriveFrom(D, SubjectDecl)) { | 
|  | 1847 | auto It = SubjectsToRules.find(D); | 
|  | 1848 | if (It == SubjectsToRules.end()) | 
|  | 1849 | continue; | 
|  | 1850 | if (!It->second.isRule() || It->second.getRule().isSubRule()) | 
|  | 1851 | continue; // Assume that the rule will be included as well. | 
|  | 1852 | Rules.push_back(It->second.getRule()); | 
|  | 1853 | } | 
|  | 1854 | } | 
|  | 1855 |  | 
|  | 1856 | bool Inserted = | 
|  | 1857 | SubjectsToRules | 
|  | 1858 | .try_emplace(SubjectDecl, | 
|  | 1859 | RuleOrAggregateRuleSet::getAggregateRuleSet(Rules)) | 
|  | 1860 | .second; | 
|  | 1861 | if (!Inserted) { | 
|  | 1862 | PrintFatalError("Attribute subject match rules should not represent" | 
|  | 1863 | "same attribute subjects."); | 
|  | 1864 | } | 
|  | 1865 | } | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1866 | } | 
|  | 1867 |  | 
|  | 1868 | static PragmaClangAttributeSupport & | 
|  | 1869 | getPragmaAttributeSupport(RecordKeeper &Records) { | 
|  | 1870 | static PragmaClangAttributeSupport Instance(Records); | 
|  | 1871 | return Instance; | 
|  | 1872 | } | 
|  | 1873 |  | 
|  | 1874 | void PragmaClangAttributeSupport::emitMatchRuleList(raw_ostream &OS) { | 
|  | 1875 | OS << "#ifndef ATTR_MATCH_SUB_RULE\n"; | 
|  | 1876 | OS << "#define ATTR_MATCH_SUB_RULE(Value, Spelling, IsAbstract, Parent, " | 
|  | 1877 | "IsNegated) " | 
|  | 1878 | << "ATTR_MATCH_RULE(Value, Spelling, IsAbstract)\n"; | 
|  | 1879 | OS << "#endif\n"; | 
|  | 1880 | for (const auto &Rule : Rules) { | 
|  | 1881 | OS << (Rule.isSubRule() ? "ATTR_MATCH_SUB_RULE" : "ATTR_MATCH_RULE") << '('; | 
|  | 1882 | OS << Rule.getEnumValueName() << ", \"" << Rule.getSpelling() << "\", " | 
|  | 1883 | << Rule.isAbstractRule(); | 
|  | 1884 | if (Rule.isSubRule()) | 
|  | 1885 | OS << ", " | 
|  | 1886 | << AttributeSubjectMatchRule(Rule.MetaSubject, nullptr).getEnumValue() | 
|  | 1887 | << ", " << Rule.isNegatedSubRule(); | 
|  | 1888 | OS << ")\n"; | 
|  | 1889 | } | 
|  | 1890 | OS << "#undef ATTR_MATCH_SUB_RULE\n"; | 
|  | 1891 | } | 
|  | 1892 |  | 
|  | 1893 | bool PragmaClangAttributeSupport::isAttributedSupported( | 
|  | 1894 | const Record &Attribute) { | 
| Richard Smith | 1bb6453 | 2018-08-30 01:01:07 +0000 | [diff] [blame] | 1895 | // If the attribute explicitly specified whether to support #pragma clang | 
|  | 1896 | // attribute, use that setting. | 
|  | 1897 | bool Unset; | 
|  | 1898 | bool SpecifiedResult = | 
|  | 1899 | Attribute.getValueAsBitOrUnset("PragmaAttributeSupport", Unset); | 
|  | 1900 | if (!Unset) | 
|  | 1901 | return SpecifiedResult; | 
|  | 1902 |  | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1903 | // Opt-out rules: | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1904 | // An attribute requires delayed parsing (LateParsed is on) | 
|  | 1905 | if (Attribute.getValueAsBit("LateParsed")) | 
|  | 1906 | return false; | 
|  | 1907 | // An attribute has no GNU/CXX11 spelling | 
|  | 1908 | if (!hasGNUorCXX11Spelling(Attribute)) | 
|  | 1909 | return false; | 
|  | 1910 | // An attribute subject list has a subject that isn't covered by one of the | 
|  | 1911 | // subject match rules or has no subjects at all. | 
|  | 1912 | if (Attribute.isValueUnset("Subjects")) | 
|  | 1913 | return false; | 
|  | 1914 | const Record *SubjectObj = Attribute.getValueAsDef("Subjects"); | 
|  | 1915 | std::vector<Record *> Subjects = SubjectObj->getValueAsListOfDefs("Subjects"); | 
|  | 1916 | if (Subjects.empty()) | 
|  | 1917 | return false; | 
|  | 1918 | for (const auto *Subject : Subjects) { | 
|  | 1919 | if (SubjectsToRules.find(Subject) == SubjectsToRules.end()) | 
|  | 1920 | return false; | 
|  | 1921 | } | 
|  | 1922 | return true; | 
|  | 1923 | } | 
|  | 1924 |  | 
| John McCall | 2c91c3b | 2019-05-30 04:09:01 +0000 | [diff] [blame] | 1925 | static std::string GenerateTestExpression(ArrayRef<Record *> LangOpts) { | 
|  | 1926 | std::string Test; | 
|  | 1927 |  | 
|  | 1928 | for (auto *E : LangOpts) { | 
|  | 1929 | if (!Test.empty()) | 
|  | 1930 | Test += " || "; | 
|  | 1931 |  | 
|  | 1932 | const StringRef Code = E->getValueAsString("CustomCode"); | 
|  | 1933 | if (!Code.empty()) { | 
|  | 1934 | Test += "("; | 
|  | 1935 | Test += Code; | 
|  | 1936 | Test += ")"; | 
|  | 1937 | } else { | 
|  | 1938 | Test += "LangOpts."; | 
|  | 1939 | Test += E->getValueAsString("Name"); | 
|  | 1940 | } | 
|  | 1941 | } | 
|  | 1942 |  | 
|  | 1943 | if (Test.empty()) | 
|  | 1944 | return "true"; | 
|  | 1945 |  | 
|  | 1946 | return Test; | 
|  | 1947 | } | 
|  | 1948 |  | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1949 | std::string | 
|  | 1950 | PragmaClangAttributeSupport::generateStrictConformsTo(const Record &Attr, | 
|  | 1951 | raw_ostream &OS) { | 
|  | 1952 | if (!isAttributedSupported(Attr)) | 
|  | 1953 | return "nullptr"; | 
|  | 1954 | // Generate a function that constructs a set of matching rules that describe | 
|  | 1955 | // to which declarations the attribute should apply to. | 
|  | 1956 | std::string FnName = "matchRulesFor" + Attr.getName().str(); | 
| Erich Keane | 3bff414 | 2017-10-16 23:25:24 +0000 | [diff] [blame] | 1957 | OS << "static void " << FnName << "(llvm::SmallVectorImpl<std::pair<" | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1958 | << AttributeSubjectMatchRule::EnumName | 
|  | 1959 | << ", bool>> &MatchRules, const LangOptions &LangOpts) {\n"; | 
|  | 1960 | if (Attr.isValueUnset("Subjects")) { | 
| Erich Keane | 3bff414 | 2017-10-16 23:25:24 +0000 | [diff] [blame] | 1961 | OS << "}\n\n"; | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1962 | return FnName; | 
|  | 1963 | } | 
|  | 1964 | const Record *SubjectObj = Attr.getValueAsDef("Subjects"); | 
|  | 1965 | std::vector<Record *> Subjects = SubjectObj->getValueAsListOfDefs("Subjects"); | 
|  | 1966 | for (const auto *Subject : Subjects) { | 
|  | 1967 | auto It = SubjectsToRules.find(Subject); | 
|  | 1968 | assert(It != SubjectsToRules.end() && | 
|  | 1969 | "This attribute is unsupported by #pragma clang attribute"); | 
| Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 1970 | for (const auto &Rule : It->getSecond().getAggregateRuleSet()) { | 
|  | 1971 | // The rule might be language specific, so only subtract it from the given | 
|  | 1972 | // rules if the specific language options are specified. | 
|  | 1973 | std::vector<Record *> LangOpts = Rule.getLangOpts(); | 
| Erich Keane | 3bff414 | 2017-10-16 23:25:24 +0000 | [diff] [blame] | 1974 | OS << "  MatchRules.push_back(std::make_pair(" << Rule.getEnumValue() | 
| John McCall | 2c91c3b | 2019-05-30 04:09:01 +0000 | [diff] [blame] | 1975 | << ", /*IsSupported=*/" << GenerateTestExpression(LangOpts) | 
|  | 1976 | << "));\n"; | 
| Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 1977 | } | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1978 | } | 
| Erich Keane | 3bff414 | 2017-10-16 23:25:24 +0000 | [diff] [blame] | 1979 | OS << "}\n\n"; | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 1980 | return FnName; | 
|  | 1981 | } | 
|  | 1982 |  | 
|  | 1983 | void PragmaClangAttributeSupport::generateParsingHelpers(raw_ostream &OS) { | 
|  | 1984 | // Generate routines that check the names of sub-rules. | 
|  | 1985 | OS << "Optional<attr::SubjectMatchRule> " | 
|  | 1986 | "defaultIsAttributeSubjectMatchSubRuleFor(StringRef, bool) {\n"; | 
|  | 1987 | OS << "  return None;\n"; | 
|  | 1988 | OS << "}\n\n"; | 
|  | 1989 |  | 
|  | 1990 | std::map<const Record *, std::vector<AttributeSubjectMatchRule>> | 
|  | 1991 | SubMatchRules; | 
|  | 1992 | for (const auto &Rule : Rules) { | 
|  | 1993 | if (!Rule.isSubRule()) | 
|  | 1994 | continue; | 
|  | 1995 | SubMatchRules[Rule.MetaSubject].push_back(Rule); | 
|  | 1996 | } | 
|  | 1997 |  | 
|  | 1998 | for (const auto &SubMatchRule : SubMatchRules) { | 
|  | 1999 | OS << "Optional<attr::SubjectMatchRule> isAttributeSubjectMatchSubRuleFor_" | 
|  | 2000 | << SubMatchRule.first->getValueAsString("Name") | 
|  | 2001 | << "(StringRef Name, bool IsUnless) {\n"; | 
|  | 2002 | OS << "  if (IsUnless)\n"; | 
|  | 2003 | OS << "    return " | 
|  | 2004 | "llvm::StringSwitch<Optional<attr::SubjectMatchRule>>(Name).\n"; | 
|  | 2005 | for (const auto &Rule : SubMatchRule.second) { | 
|  | 2006 | if (Rule.isNegatedSubRule()) | 
|  | 2007 | OS << "    Case(\"" << Rule.getName() << "\", " << Rule.getEnumValue() | 
|  | 2008 | << ").\n"; | 
|  | 2009 | } | 
|  | 2010 | OS << "    Default(None);\n"; | 
|  | 2011 | OS << "  return " | 
|  | 2012 | "llvm::StringSwitch<Optional<attr::SubjectMatchRule>>(Name).\n"; | 
|  | 2013 | for (const auto &Rule : SubMatchRule.second) { | 
|  | 2014 | if (!Rule.isNegatedSubRule()) | 
|  | 2015 | OS << "  Case(\"" << Rule.getName() << "\", " << Rule.getEnumValue() | 
|  | 2016 | << ").\n"; | 
|  | 2017 | } | 
|  | 2018 | OS << "  Default(None);\n"; | 
|  | 2019 | OS << "}\n\n"; | 
|  | 2020 | } | 
|  | 2021 |  | 
|  | 2022 | // Generate the function that checks for the top-level rules. | 
|  | 2023 | OS << "std::pair<Optional<attr::SubjectMatchRule>, " | 
|  | 2024 | "Optional<attr::SubjectMatchRule> (*)(StringRef, " | 
|  | 2025 | "bool)> isAttributeSubjectMatchRule(StringRef Name) {\n"; | 
|  | 2026 | OS << "  return " | 
|  | 2027 | "llvm::StringSwitch<std::pair<Optional<attr::SubjectMatchRule>, " | 
|  | 2028 | "Optional<attr::SubjectMatchRule> (*) (StringRef, " | 
|  | 2029 | "bool)>>(Name).\n"; | 
|  | 2030 | for (const auto &Rule : Rules) { | 
|  | 2031 | if (Rule.isSubRule()) | 
|  | 2032 | continue; | 
|  | 2033 | std::string SubRuleFunction; | 
|  | 2034 | if (SubMatchRules.count(Rule.MetaSubject)) | 
| Erich Keane | 3bff414 | 2017-10-16 23:25:24 +0000 | [diff] [blame] | 2035 | SubRuleFunction = | 
|  | 2036 | ("isAttributeSubjectMatchSubRuleFor_" + Rule.getName()).str(); | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 2037 | else | 
|  | 2038 | SubRuleFunction = "defaultIsAttributeSubjectMatchSubRuleFor"; | 
|  | 2039 | OS << "  Case(\"" << Rule.getName() << "\", std::make_pair(" | 
|  | 2040 | << Rule.getEnumValue() << ", " << SubRuleFunction << ")).\n"; | 
|  | 2041 | } | 
|  | 2042 | OS << "  Default(std::make_pair(None, " | 
|  | 2043 | "defaultIsAttributeSubjectMatchSubRuleFor));\n"; | 
|  | 2044 | OS << "}\n\n"; | 
|  | 2045 |  | 
|  | 2046 | // Generate the function that checks for the submatch rules. | 
|  | 2047 | OS << "const char *validAttributeSubjectMatchSubRules(" | 
|  | 2048 | << AttributeSubjectMatchRule::EnumName << " Rule) {\n"; | 
|  | 2049 | OS << "  switch (Rule) {\n"; | 
|  | 2050 | for (const auto &SubMatchRule : SubMatchRules) { | 
|  | 2051 | OS << "  case " | 
|  | 2052 | << AttributeSubjectMatchRule(SubMatchRule.first, nullptr).getEnumValue() | 
|  | 2053 | << ":\n"; | 
|  | 2054 | OS << "  return \"'"; | 
|  | 2055 | bool IsFirst = true; | 
|  | 2056 | for (const auto &Rule : SubMatchRule.second) { | 
|  | 2057 | if (!IsFirst) | 
|  | 2058 | OS << ", '"; | 
|  | 2059 | IsFirst = false; | 
|  | 2060 | if (Rule.isNegatedSubRule()) | 
|  | 2061 | OS << "unless("; | 
|  | 2062 | OS << Rule.getName(); | 
|  | 2063 | if (Rule.isNegatedSubRule()) | 
|  | 2064 | OS << ')'; | 
|  | 2065 | OS << "'"; | 
|  | 2066 | } | 
|  | 2067 | OS << "\";\n"; | 
|  | 2068 | } | 
|  | 2069 | OS << "  default: return nullptr;\n"; | 
|  | 2070 | OS << "  }\n"; | 
|  | 2071 | OS << "}\n\n"; | 
|  | 2072 | } | 
|  | 2073 |  | 
| George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 2074 | template <typename Fn> | 
|  | 2075 | static void forEachUniqueSpelling(const Record &Attr, Fn &&F) { | 
|  | 2076 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr); | 
|  | 2077 | SmallDenseSet<StringRef, 8> Seen; | 
|  | 2078 | for (const FlattenedSpelling &S : Spellings) { | 
|  | 2079 | if (Seen.insert(S.name()).second) | 
|  | 2080 | F(S); | 
|  | 2081 | } | 
|  | 2082 | } | 
|  | 2083 |  | 
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2084 | /// Emits the first-argument-is-type property for attributes. | 
| Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 2085 | static void emitClangAttrTypeArgList(RecordKeeper &Records, raw_ostream &OS) { | 
|  | 2086 | OS << "#if defined(CLANG_ATTR_TYPE_ARG_LIST)\n"; | 
|  | 2087 | std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); | 
|  | 2088 |  | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2089 | for (const auto *Attr : Attrs) { | 
| Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 2090 | // Determine whether the first argument is a type. | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 2091 | std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args"); | 
| Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 2092 | if (Args.empty()) | 
|  | 2093 | continue; | 
|  | 2094 |  | 
| Craig Topper | 2576124 | 2016-01-18 19:52:54 +0000 | [diff] [blame] | 2095 | if (Args[0]->getSuperClasses().back().first->getName() != "TypeArgument") | 
| Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 2096 | continue; | 
|  | 2097 |  | 
|  | 2098 | // All these spellings take a single type argument. | 
| George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 2099 | forEachUniqueSpelling(*Attr, [&](const FlattenedSpelling &S) { | 
|  | 2100 | OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n"; | 
|  | 2101 | }); | 
| Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 2102 | } | 
|  | 2103 | OS << "#endif // CLANG_ATTR_TYPE_ARG_LIST\n\n"; | 
|  | 2104 | } | 
|  | 2105 |  | 
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2106 | /// Emits the parse-arguments-in-unevaluated-context property for | 
| Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 2107 | /// attributes. | 
|  | 2108 | static void emitClangAttrArgContextList(RecordKeeper &Records, raw_ostream &OS) { | 
|  | 2109 | OS << "#if defined(CLANG_ATTR_ARG_CONTEXT_LIST)\n"; | 
|  | 2110 | ParsedAttrMap Attrs = getParsedAttrList(Records); | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 2111 | for (const auto &I : Attrs) { | 
|  | 2112 | const Record &Attr = *I.second; | 
| Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 2113 |  | 
|  | 2114 | if (!Attr.getValueAsBit("ParseArgumentsAsUnevaluated")) | 
|  | 2115 | continue; | 
|  | 2116 |  | 
|  | 2117 | // All these spellings take are parsed unevaluated. | 
| George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 2118 | forEachUniqueSpelling(Attr, [&](const FlattenedSpelling &S) { | 
|  | 2119 | OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n"; | 
|  | 2120 | }); | 
| Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 2121 | } | 
|  | 2122 | OS << "#endif // CLANG_ATTR_ARG_CONTEXT_LIST\n\n"; | 
|  | 2123 | } | 
|  | 2124 |  | 
|  | 2125 | static bool isIdentifierArgument(Record *Arg) { | 
|  | 2126 | return !Arg->getSuperClasses().empty() && | 
| Craig Topper | 2576124 | 2016-01-18 19:52:54 +0000 | [diff] [blame] | 2127 | llvm::StringSwitch<bool>(Arg->getSuperClasses().back().first->getName()) | 
| Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 2128 | .Case("IdentifierArgument", true) | 
|  | 2129 | .Case("EnumArgument", true) | 
| Aaron Ballman | 55ef151 | 2014-12-19 16:42:04 +0000 | [diff] [blame] | 2130 | .Case("VariadicEnumArgument", true) | 
| Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 2131 | .Default(false); | 
|  | 2132 | } | 
|  | 2133 |  | 
| Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 2134 | static bool isVariadicIdentifierArgument(Record *Arg) { | 
|  | 2135 | return !Arg->getSuperClasses().empty() && | 
|  | 2136 | llvm::StringSwitch<bool>( | 
|  | 2137 | Arg->getSuperClasses().back().first->getName()) | 
|  | 2138 | .Case("VariadicIdentifierArgument", true) | 
| Johannes Doerfert | ac991bb | 2019-01-19 05:36:54 +0000 | [diff] [blame] | 2139 | .Case("VariadicParamOrParamIdxArgument", true) | 
| Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 2140 | .Default(false); | 
|  | 2141 | } | 
|  | 2142 |  | 
|  | 2143 | static void emitClangAttrVariadicIdentifierArgList(RecordKeeper &Records, | 
|  | 2144 | raw_ostream &OS) { | 
|  | 2145 | OS << "#if defined(CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST)\n"; | 
|  | 2146 | std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); | 
|  | 2147 | for (const auto *A : Attrs) { | 
|  | 2148 | // Determine whether the first argument is a variadic identifier. | 
|  | 2149 | std::vector<Record *> Args = A->getValueAsListOfDefs("Args"); | 
|  | 2150 | if (Args.empty() || !isVariadicIdentifierArgument(Args[0])) | 
|  | 2151 | continue; | 
|  | 2152 |  | 
|  | 2153 | // All these spellings take an identifier argument. | 
|  | 2154 | forEachUniqueSpelling(*A, [&](const FlattenedSpelling &S) { | 
|  | 2155 | OS << ".Case(\"" << S.name() << "\", " | 
|  | 2156 | << "true" | 
|  | 2157 | << ")\n"; | 
|  | 2158 | }); | 
|  | 2159 | } | 
|  | 2160 | OS << "#endif // CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST\n\n"; | 
|  | 2161 | } | 
|  | 2162 |  | 
| Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 2163 | // Emits the first-argument-is-identifier property for attributes. | 
|  | 2164 | static void emitClangAttrIdentifierArgList(RecordKeeper &Records, raw_ostream &OS) { | 
|  | 2165 | OS << "#if defined(CLANG_ATTR_IDENTIFIER_ARG_LIST)\n"; | 
|  | 2166 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"); | 
|  | 2167 |  | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2168 | for (const auto *Attr : Attrs) { | 
| Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 2169 | // Determine whether the first argument is an identifier. | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 2170 | std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args"); | 
| Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 2171 | if (Args.empty() || !isIdentifierArgument(Args[0])) | 
|  | 2172 | continue; | 
|  | 2173 |  | 
|  | 2174 | // All these spellings take an identifier argument. | 
| George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 2175 | forEachUniqueSpelling(*Attr, [&](const FlattenedSpelling &S) { | 
|  | 2176 | OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n"; | 
|  | 2177 | }); | 
| Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 2178 | } | 
|  | 2179 | OS << "#endif // CLANG_ATTR_IDENTIFIER_ARG_LIST\n\n"; | 
|  | 2180 | } | 
|  | 2181 |  | 
| Johannes Doerfert | ac991bb | 2019-01-19 05:36:54 +0000 | [diff] [blame] | 2182 | static bool keywordThisIsaIdentifierInArgument(const Record *Arg) { | 
|  | 2183 | return !Arg->getSuperClasses().empty() && | 
|  | 2184 | llvm::StringSwitch<bool>( | 
|  | 2185 | Arg->getSuperClasses().back().first->getName()) | 
|  | 2186 | .Case("VariadicParamOrParamIdxArgument", true) | 
|  | 2187 | .Default(false); | 
|  | 2188 | } | 
|  | 2189 |  | 
|  | 2190 | static void emitClangAttrThisIsaIdentifierArgList(RecordKeeper &Records, | 
|  | 2191 | raw_ostream &OS) { | 
|  | 2192 | OS << "#if defined(CLANG_ATTR_THIS_ISA_IDENTIFIER_ARG_LIST)\n"; | 
|  | 2193 | std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); | 
|  | 2194 | for (const auto *A : Attrs) { | 
|  | 2195 | // Determine whether the first argument is a variadic identifier. | 
|  | 2196 | std::vector<Record *> Args = A->getValueAsListOfDefs("Args"); | 
|  | 2197 | if (Args.empty() || !keywordThisIsaIdentifierInArgument(Args[0])) | 
|  | 2198 | continue; | 
|  | 2199 |  | 
|  | 2200 | // All these spellings take an identifier argument. | 
|  | 2201 | forEachUniqueSpelling(*A, [&](const FlattenedSpelling &S) { | 
|  | 2202 | OS << ".Case(\"" << S.name() << "\", " | 
|  | 2203 | << "true" | 
|  | 2204 | << ")\n"; | 
|  | 2205 | }); | 
|  | 2206 | } | 
|  | 2207 | OS << "#endif // CLANG_ATTR_THIS_ISA_IDENTIFIER_ARG_LIST\n\n"; | 
|  | 2208 | } | 
|  | 2209 |  | 
| Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 2210 | namespace clang { | 
|  | 2211 |  | 
|  | 2212 | // Emits the class definitions for attributes. | 
|  | 2213 | void EmitClangAttrClass(RecordKeeper &Records, raw_ostream &OS) { | 
| Dmitri Gribenko | 6b11fca | 2013-01-30 21:54:20 +0000 | [diff] [blame] | 2214 | emitSourceFileHeader("Attribute classes' definitions", OS); | 
|  | 2215 |  | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2216 | OS << "#ifndef LLVM_CLANG_ATTR_CLASSES_INC\n"; | 
|  | 2217 | OS << "#define LLVM_CLANG_ATTR_CLASSES_INC\n\n"; | 
|  | 2218 |  | 
|  | 2219 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"); | 
|  | 2220 |  | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2221 | for (const auto *Attr : Attrs) { | 
|  | 2222 | const Record &R = *Attr; | 
| Aaron Ballman | 06bd44b | 2014-02-17 18:23:02 +0000 | [diff] [blame] | 2223 |  | 
|  | 2224 | // FIXME: Currently, documentation is generated as-needed due to the fact | 
|  | 2225 | // that there is no way to allow a generated project "reach into" the docs | 
|  | 2226 | // directory (for instance, it may be an out-of-tree build). However, we want | 
|  | 2227 | // to ensure that every attribute has a Documentation field, and produce an | 
|  | 2228 | // error if it has been neglected. Otherwise, the on-demand generation which | 
|  | 2229 | // happens server-side will fail. This code is ensuring that functionality, | 
|  | 2230 | // even though this Emitter doesn't technically need the documentation. | 
|  | 2231 | // When attribute documentation can be generated as part of the build | 
|  | 2232 | // itself, this code can be removed. | 
|  | 2233 | (void)R.getValueAsListOfDefs("Documentation"); | 
| Douglas Gregor | b2daf84 | 2012-05-02 15:56:52 +0000 | [diff] [blame] | 2234 |  | 
|  | 2235 | if (!R.getValueAsBit("ASTNode")) | 
|  | 2236 | continue; | 
|  | 2237 |  | 
| Craig Topper | 2576124 | 2016-01-18 19:52:54 +0000 | [diff] [blame] | 2238 | ArrayRef<std::pair<Record *, SMRange>> Supers = R.getSuperClasses(); | 
| Aaron Ballman | 0979e9e | 2013-07-30 01:44:15 +0000 | [diff] [blame] | 2239 | assert(!Supers.empty() && "Forgot to specify a superclass for the attr"); | 
| Aaron Ballman | 0979e9e | 2013-07-30 01:44:15 +0000 | [diff] [blame] | 2240 | std::string SuperName; | 
| Richard Smith | 33bddbd | 2018-01-04 23:42:29 +0000 | [diff] [blame] | 2241 | bool Inheritable = false; | 
| David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2242 | for (const auto &Super : llvm::reverse(Supers)) { | 
| Craig Topper | 2576124 | 2016-01-18 19:52:54 +0000 | [diff] [blame] | 2243 | const Record *R = Super.first; | 
| Aaron Ballman | b9a457a | 2018-05-03 15:33:50 +0000 | [diff] [blame] | 2244 | if (R->getName() != "TargetSpecificAttr" && | 
|  | 2245 | R->getName() != "DeclOrTypeAttr" && SuperName.empty()) | 
| Craig Topper | 2576124 | 2016-01-18 19:52:54 +0000 | [diff] [blame] | 2246 | SuperName = R->getName(); | 
| Richard Smith | 33bddbd | 2018-01-04 23:42:29 +0000 | [diff] [blame] | 2247 | if (R->getName() == "InheritableAttr") | 
|  | 2248 | Inheritable = true; | 
| Aaron Ballman | 0979e9e | 2013-07-30 01:44:15 +0000 | [diff] [blame] | 2249 | } | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2250 |  | 
|  | 2251 | OS << "class " << R.getName() << "Attr : public " << SuperName << " {\n"; | 
|  | 2252 |  | 
|  | 2253 | std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args"); | 
| Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2254 | std::vector<std::unique_ptr<Argument>> Args; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2255 | Args.reserve(ArgRecords.size()); | 
|  | 2256 |  | 
| John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 2257 | bool HasOptArg = false; | 
|  | 2258 | bool HasFakeArg = false; | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2259 | for (const auto *ArgRecord : ArgRecords) { | 
| Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2260 | Args.emplace_back(createArgument(*ArgRecord, R.getName())); | 
|  | 2261 | Args.back()->writeDeclarations(OS); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2262 | OS << "\n\n"; | 
| John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 2263 |  | 
|  | 2264 | // For these purposes, fake takes priority over optional. | 
|  | 2265 | if (Args.back()->isFake()) { | 
|  | 2266 | HasFakeArg = true; | 
|  | 2267 | } else if (Args.back()->isOptional()) { | 
|  | 2268 | HasOptArg = true; | 
|  | 2269 | } | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2270 | } | 
|  | 2271 |  | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 2272 | OS << "public:\n"; | 
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2273 |  | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 2274 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R); | 
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2275 |  | 
|  | 2276 | // If there are zero or one spellings, all spelling-related functionality | 
|  | 2277 | // can be elided. If all of the spellings share the same name, the spelling | 
|  | 2278 | // functionality can also be elided. | 
|  | 2279 | bool ElideSpelling = (Spellings.size() <= 1) || | 
|  | 2280 | SpellingNamesAreCommon(Spellings); | 
|  | 2281 |  | 
| Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 2282 | // This maps spelling index values to semantic Spelling enumerants. | 
|  | 2283 | SemanticSpellingMap SemanticToSyntacticMap; | 
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2284 |  | 
| Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 2285 | if (!ElideSpelling) | 
|  | 2286 | OS << CreateSemanticSpellings(Spellings, SemanticToSyntacticMap); | 
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2287 |  | 
| John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 2288 | // Emit CreateImplicit factory methods. | 
|  | 2289 | auto emitCreateImplicit = [&](bool emitFake) { | 
|  | 2290 | OS << "  static " << R.getName() << "Attr *CreateImplicit("; | 
|  | 2291 | OS << "ASTContext &Ctx"; | 
|  | 2292 | if (!ElideSpelling) | 
|  | 2293 | OS << ", Spelling S"; | 
|  | 2294 | for (auto const &ai : Args) { | 
|  | 2295 | if (ai->isFake() && !emitFake) continue; | 
|  | 2296 | OS << ", "; | 
|  | 2297 | ai->writeCtorParameters(OS); | 
|  | 2298 | } | 
|  | 2299 | OS << ", SourceRange Loc = SourceRange()"; | 
|  | 2300 | OS << ") {\n"; | 
| Eugene Zelenko | 5f02b77 | 2015-12-08 18:49:01 +0000 | [diff] [blame] | 2301 | OS << "    auto *A = new (Ctx) " << R.getName(); | 
| John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 2302 | OS << "Attr(Loc, Ctx, "; | 
|  | 2303 | for (auto const &ai : Args) { | 
|  | 2304 | if (ai->isFake() && !emitFake) continue; | 
|  | 2305 | ai->writeImplicitCtorArgs(OS); | 
|  | 2306 | OS << ", "; | 
|  | 2307 | } | 
|  | 2308 | OS << (ElideSpelling ? "0" : "S") << ");\n"; | 
|  | 2309 | OS << "    A->setImplicit(true);\n"; | 
|  | 2310 | OS << "    return A;\n  }\n\n"; | 
|  | 2311 | }; | 
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2312 |  | 
| John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 2313 | // Emit a CreateImplicit that takes all the arguments. | 
|  | 2314 | emitCreateImplicit(true); | 
|  | 2315 |  | 
|  | 2316 | // Emit a CreateImplicit that takes all the non-fake arguments. | 
|  | 2317 | if (HasFakeArg) { | 
|  | 2318 | emitCreateImplicit(false); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2319 | } | 
| Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2320 |  | 
| John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 2321 | // Emit constructors. | 
|  | 2322 | auto emitCtor = [&](bool emitOpt, bool emitFake) { | 
|  | 2323 | auto shouldEmitArg = [=](const std::unique_ptr<Argument> &arg) { | 
|  | 2324 | if (arg->isFake()) return emitFake; | 
|  | 2325 | if (arg->isOptional()) return emitOpt; | 
|  | 2326 | return true; | 
|  | 2327 | }; | 
| Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2328 |  | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 2329 | OS << "  " << R.getName() << "Attr(SourceRange R, ASTContext &Ctx\n"; | 
| Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2330 | for (auto const &ai : Args) { | 
| John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 2331 | if (!shouldEmitArg(ai)) continue; | 
|  | 2332 | OS << "              , "; | 
|  | 2333 | ai->writeCtorParameters(OS); | 
|  | 2334 | OS << "\n"; | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 2335 | } | 
|  | 2336 |  | 
|  | 2337 | OS << "              , "; | 
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2338 | OS << "unsigned SI\n"; | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 2339 |  | 
|  | 2340 | OS << "             )\n"; | 
| Benjamin Kramer | 845e32c | 2015-03-19 16:06:49 +0000 | [diff] [blame] | 2341 | OS << "    : " << SuperName << "(attr::" << R.getName() << ", R, SI, " | 
| Richard Smith | 33bddbd | 2018-01-04 23:42:29 +0000 | [diff] [blame] | 2342 | << ( R.getValueAsBit("LateParsed") ? "true" : "false" ); | 
|  | 2343 | if (Inheritable) { | 
|  | 2344 | OS << ", " | 
|  | 2345 | << (R.getValueAsBit("InheritEvenIfAlreadyPresent") ? "true" | 
|  | 2346 | : "false"); | 
|  | 2347 | } | 
|  | 2348 | OS << ")\n"; | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 2349 |  | 
| Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2350 | for (auto const &ai : Args) { | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 2351 | OS << "              , "; | 
| John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 2352 | if (!shouldEmitArg(ai)) { | 
|  | 2353 | ai->writeCtorDefaultInitializers(OS); | 
|  | 2354 | } else { | 
|  | 2355 | ai->writeCtorInitializers(OS); | 
|  | 2356 | } | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 2357 | OS << "\n"; | 
|  | 2358 | } | 
|  | 2359 |  | 
|  | 2360 | OS << "  {\n"; | 
|  | 2361 |  | 
| Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2362 | for (auto const &ai : Args) { | 
| John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 2363 | if (!shouldEmitArg(ai)) continue; | 
|  | 2364 | ai->writeCtorBody(OS); | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 2365 | } | 
|  | 2366 | OS << "  }\n\n"; | 
| John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 2367 | }; | 
|  | 2368 |  | 
|  | 2369 | // Emit a constructor that includes all the arguments. | 
|  | 2370 | // This is necessary for cloning. | 
|  | 2371 | emitCtor(true, true); | 
|  | 2372 |  | 
|  | 2373 | // Emit a constructor that takes all the non-fake arguments. | 
|  | 2374 | if (HasFakeArg) { | 
|  | 2375 | emitCtor(true, false); | 
|  | 2376 | } | 
|  | 2377 |  | 
|  | 2378 | // Emit a constructor that takes all the non-fake, non-optional arguments. | 
|  | 2379 | if (HasOptArg) { | 
|  | 2380 | emitCtor(false, false); | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 2381 | } | 
|  | 2382 |  | 
| Benjamin Kramer | 845e32c | 2015-03-19 16:06:49 +0000 | [diff] [blame] | 2383 | OS << "  " << R.getName() << "Attr *clone(ASTContext &C) const;\n"; | 
| Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 2384 | OS << "  void printPretty(raw_ostream &OS,\n" | 
| Benjamin Kramer | 845e32c | 2015-03-19 16:06:49 +0000 | [diff] [blame] | 2385 | << "                   const PrintingPolicy &Policy) const;\n"; | 
|  | 2386 | OS << "  const char *getSpelling() const;\n"; | 
| Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 2387 |  | 
|  | 2388 | if (!ElideSpelling) { | 
|  | 2389 | assert(!SemanticToSyntacticMap.empty() && "Empty semantic mapping list"); | 
|  | 2390 | OS << "  Spelling getSemanticSpelling() const {\n"; | 
|  | 2391 | WriteSemanticSpellingSwitch("SpellingListIndex", SemanticToSyntacticMap, | 
|  | 2392 | OS); | 
|  | 2393 | OS << "  }\n"; | 
|  | 2394 | } | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2395 |  | 
| Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2396 | writeAttrAccessorDefinition(R, OS); | 
|  | 2397 |  | 
| Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2398 | for (auto const &ai : Args) { | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 2399 | ai->writeAccessors(OS); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2400 | OS << "\n\n"; | 
| Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2401 |  | 
| John McCall | a62c1a9 | 2015-10-28 00:17:34 +0000 | [diff] [blame] | 2402 | // Don't write conversion routines for fake arguments. | 
|  | 2403 | if (ai->isFake()) continue; | 
|  | 2404 |  | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 2405 | if (ai->isEnumArg()) | 
| Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2406 | static_cast<const EnumArgument *>(ai.get())->writeConversion(OS); | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 2407 | else if (ai->isVariadicEnumArg()) | 
| Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2408 | static_cast<const VariadicEnumArgument *>(ai.get()) | 
|  | 2409 | ->writeConversion(OS); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2410 | } | 
|  | 2411 |  | 
| Jakob Stoklund Olesen | 6f2288b6 | 2012-01-13 04:57:47 +0000 | [diff] [blame] | 2412 | OS << R.getValueAsString("AdditionalMembers"); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2413 | OS << "\n\n"; | 
|  | 2414 |  | 
|  | 2415 | OS << "  static bool classof(const Attr *A) { return A->getKind() == " | 
|  | 2416 | << "attr::" << R.getName() << "; }\n"; | 
| DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 2417 |  | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2418 | OS << "};\n\n"; | 
|  | 2419 | } | 
|  | 2420 |  | 
| Eugene Zelenko | 5f02b77 | 2015-12-08 18:49:01 +0000 | [diff] [blame] | 2421 | OS << "#endif // LLVM_CLANG_ATTR_CLASSES_INC\n"; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2422 | } | 
|  | 2423 |  | 
| Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 2424 | // Emits the class method definitions for attributes. | 
|  | 2425 | void EmitClangAttrImpl(RecordKeeper &Records, raw_ostream &OS) { | 
| Dmitri Gribenko | 6b11fca | 2013-01-30 21:54:20 +0000 | [diff] [blame] | 2426 | emitSourceFileHeader("Attribute classes' member function definitions", OS); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2427 |  | 
|  | 2428 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2429 |  | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2430 | for (auto *Attr : Attrs) { | 
|  | 2431 | Record &R = *Attr; | 
| Douglas Gregor | b2daf84 | 2012-05-02 15:56:52 +0000 | [diff] [blame] | 2432 |  | 
|  | 2433 | if (!R.getValueAsBit("ASTNode")) | 
|  | 2434 | continue; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2435 |  | 
| Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2436 | std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args"); | 
|  | 2437 | std::vector<std::unique_ptr<Argument>> Args; | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2438 | for (const auto *Arg : ArgRecords) | 
|  | 2439 | Args.emplace_back(createArgument(*Arg, R.getName())); | 
| Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2440 |  | 
|  | 2441 | for (auto const &ai : Args) | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 2442 | ai->writeAccessorDefinitions(OS); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2443 |  | 
|  | 2444 | OS << R.getName() << "Attr *" << R.getName() | 
|  | 2445 | << "Attr::clone(ASTContext &C) const {\n"; | 
| Hans Wennborg | 613807b | 2014-05-31 01:30:30 +0000 | [diff] [blame] | 2446 | OS << "  auto *A = new (C) " << R.getName() << "Attr(getLocation(), C"; | 
| Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2447 | for (auto const &ai : Args) { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2448 | OS << ", "; | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 2449 | ai->writeCloneArgs(OS); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2450 | } | 
| Hans Wennborg | 613807b | 2014-05-31 01:30:30 +0000 | [diff] [blame] | 2451 | OS << ", getSpellingListIndex());\n"; | 
|  | 2452 | OS << "  A->Inherited = Inherited;\n"; | 
|  | 2453 | OS << "  A->IsPackExpansion = IsPackExpansion;\n"; | 
|  | 2454 | OS << "  A->Implicit = Implicit;\n"; | 
|  | 2455 | OS << "  return A;\n}\n\n"; | 
| Douglas Gregor | 49ccfaa | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 2456 |  | 
| Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2457 | writePrettyPrintFunction(R, Args, OS); | 
| Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 2458 | writeGetSpellingFunction(R, OS); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2459 | } | 
| Benjamin Kramer | 845e32c | 2015-03-19 16:06:49 +0000 | [diff] [blame] | 2460 |  | 
|  | 2461 | // Instead of relying on virtual dispatch we just create a huge dispatch | 
|  | 2462 | // switch. This is both smaller and faster than virtual functions. | 
|  | 2463 | auto EmitFunc = [&](const char *Method) { | 
|  | 2464 | OS << "  switch (getKind()) {\n"; | 
|  | 2465 | for (const auto *Attr : Attrs) { | 
|  | 2466 | const Record &R = *Attr; | 
|  | 2467 | if (!R.getValueAsBit("ASTNode")) | 
|  | 2468 | continue; | 
|  | 2469 |  | 
|  | 2470 | OS << "  case attr::" << R.getName() << ":\n"; | 
|  | 2471 | OS << "    return cast<" << R.getName() << "Attr>(this)->" << Method | 
|  | 2472 | << ";\n"; | 
|  | 2473 | } | 
| Benjamin Kramer | 845e32c | 2015-03-19 16:06:49 +0000 | [diff] [blame] | 2474 | OS << "  }\n"; | 
|  | 2475 | OS << "  llvm_unreachable(\"Unexpected attribute kind!\");\n"; | 
|  | 2476 | OS << "}\n\n"; | 
|  | 2477 | }; | 
|  | 2478 |  | 
|  | 2479 | OS << "const char *Attr::getSpelling() const {\n"; | 
|  | 2480 | EmitFunc("getSpelling()"); | 
|  | 2481 |  | 
|  | 2482 | OS << "Attr *Attr::clone(ASTContext &C) const {\n"; | 
|  | 2483 | EmitFunc("clone(C)"); | 
|  | 2484 |  | 
|  | 2485 | OS << "void Attr::printPretty(raw_ostream &OS, " | 
|  | 2486 | "const PrintingPolicy &Policy) const {\n"; | 
|  | 2487 | EmitFunc("printPretty(OS, Policy)"); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2488 | } | 
|  | 2489 |  | 
| Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 2490 | } // end namespace clang | 
|  | 2491 |  | 
| John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2492 | static void emitAttrList(raw_ostream &OS, StringRef Class, | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2493 | const std::vector<Record*> &AttrList) { | 
| John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2494 | for (auto Cur : AttrList) { | 
|  | 2495 | OS << Class << "(" << Cur->getName() << ")\n"; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2496 | } | 
|  | 2497 | } | 
|  | 2498 |  | 
| Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 2499 | // Determines if an attribute has a Pragma spelling. | 
|  | 2500 | static bool AttrHasPragmaSpelling(const Record *R) { | 
|  | 2501 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R); | 
| George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 2502 | return llvm::find_if(Spellings, [](const FlattenedSpelling &S) { | 
| Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 2503 | return S.variety() == "Pragma"; | 
|  | 2504 | }) != Spellings.end(); | 
|  | 2505 | } | 
| Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 2506 |  | 
| John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2507 | namespace { | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 2508 |  | 
| John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2509 | struct AttrClassDescriptor { | 
|  | 2510 | const char * const MacroName; | 
|  | 2511 | const char * const TableGenName; | 
|  | 2512 | }; | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 2513 |  | 
|  | 2514 | } // end anonymous namespace | 
| John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2515 |  | 
|  | 2516 | static const AttrClassDescriptor AttrClassDescriptors[] = { | 
|  | 2517 | { "ATTR", "Attr" }, | 
| Richard Smith | e43e2b3 | 2018-08-20 21:47:29 +0000 | [diff] [blame] | 2518 | { "TYPE_ATTR", "TypeAttr" }, | 
| Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 2519 | { "STMT_ATTR", "StmtAttr" }, | 
| John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2520 | { "INHERITABLE_ATTR", "InheritableAttr" }, | 
| Richard Smith | e43e2b3 | 2018-08-20 21:47:29 +0000 | [diff] [blame] | 2521 | { "DECL_OR_TYPE_ATTR", "DeclOrTypeAttr" }, | 
| John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 2522 | { "INHERITABLE_PARAM_ATTR", "InheritableParamAttr" }, | 
|  | 2523 | { "PARAMETER_ABI_ATTR", "ParameterABIAttr" } | 
| John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2524 | }; | 
|  | 2525 |  | 
|  | 2526 | static void emitDefaultDefine(raw_ostream &OS, StringRef name, | 
|  | 2527 | const char *superName) { | 
|  | 2528 | OS << "#ifndef " << name << "\n"; | 
|  | 2529 | OS << "#define " << name << "(NAME) "; | 
|  | 2530 | if (superName) OS << superName << "(NAME)"; | 
|  | 2531 | OS << "\n#endif\n\n"; | 
|  | 2532 | } | 
|  | 2533 |  | 
|  | 2534 | namespace { | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 2535 |  | 
| John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2536 | /// A class of attributes. | 
|  | 2537 | struct AttrClass { | 
|  | 2538 | const AttrClassDescriptor &Descriptor; | 
|  | 2539 | Record *TheRecord; | 
|  | 2540 | AttrClass *SuperClass = nullptr; | 
|  | 2541 | std::vector<AttrClass*> SubClasses; | 
|  | 2542 | std::vector<Record*> Attrs; | 
|  | 2543 |  | 
|  | 2544 | AttrClass(const AttrClassDescriptor &Descriptor, Record *R) | 
|  | 2545 | : Descriptor(Descriptor), TheRecord(R) {} | 
|  | 2546 |  | 
|  | 2547 | void emitDefaultDefines(raw_ostream &OS) const { | 
|  | 2548 | // Default the macro unless this is a root class (i.e. Attr). | 
|  | 2549 | if (SuperClass) { | 
|  | 2550 | emitDefaultDefine(OS, Descriptor.MacroName, | 
|  | 2551 | SuperClass->Descriptor.MacroName); | 
|  | 2552 | } | 
|  | 2553 | } | 
|  | 2554 |  | 
|  | 2555 | void emitUndefs(raw_ostream &OS) const { | 
|  | 2556 | OS << "#undef " << Descriptor.MacroName << "\n"; | 
|  | 2557 | } | 
|  | 2558 |  | 
|  | 2559 | void emitAttrList(raw_ostream &OS) const { | 
|  | 2560 | for (auto SubClass : SubClasses) { | 
|  | 2561 | SubClass->emitAttrList(OS); | 
|  | 2562 | } | 
|  | 2563 |  | 
|  | 2564 | ::emitAttrList(OS, Descriptor.MacroName, Attrs); | 
|  | 2565 | } | 
|  | 2566 |  | 
|  | 2567 | void classifyAttrOnRoot(Record *Attr) { | 
|  | 2568 | bool result = classifyAttr(Attr); | 
|  | 2569 | assert(result && "failed to classify on root"); (void) result; | 
|  | 2570 | } | 
|  | 2571 |  | 
|  | 2572 | void emitAttrRange(raw_ostream &OS) const { | 
|  | 2573 | OS << "ATTR_RANGE(" << Descriptor.TableGenName | 
|  | 2574 | << ", " << getFirstAttr()->getName() | 
|  | 2575 | << ", " << getLastAttr()->getName() << ")\n"; | 
|  | 2576 | } | 
|  | 2577 |  | 
|  | 2578 | private: | 
|  | 2579 | bool classifyAttr(Record *Attr) { | 
|  | 2580 | // Check all the subclasses. | 
|  | 2581 | for (auto SubClass : SubClasses) { | 
|  | 2582 | if (SubClass->classifyAttr(Attr)) | 
|  | 2583 | return true; | 
|  | 2584 | } | 
|  | 2585 |  | 
|  | 2586 | // It's not more specific than this class, but it might still belong here. | 
|  | 2587 | if (Attr->isSubClassOf(TheRecord)) { | 
|  | 2588 | Attrs.push_back(Attr); | 
|  | 2589 | return true; | 
|  | 2590 | } | 
|  | 2591 |  | 
|  | 2592 | return false; | 
|  | 2593 | } | 
|  | 2594 |  | 
|  | 2595 | Record *getFirstAttr() const { | 
|  | 2596 | if (!SubClasses.empty()) | 
|  | 2597 | return SubClasses.front()->getFirstAttr(); | 
|  | 2598 | return Attrs.front(); | 
|  | 2599 | } | 
|  | 2600 |  | 
|  | 2601 | Record *getLastAttr() const { | 
|  | 2602 | if (!Attrs.empty()) | 
|  | 2603 | return Attrs.back(); | 
|  | 2604 | return SubClasses.back()->getLastAttr(); | 
|  | 2605 | } | 
|  | 2606 | }; | 
|  | 2607 |  | 
|  | 2608 | /// The entire hierarchy of attribute classes. | 
|  | 2609 | class AttrClassHierarchy { | 
|  | 2610 | std::vector<std::unique_ptr<AttrClass>> Classes; | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 2611 |  | 
| John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2612 | public: | 
|  | 2613 | AttrClassHierarchy(RecordKeeper &Records) { | 
|  | 2614 | // Find records for all the classes. | 
|  | 2615 | for (auto &Descriptor : AttrClassDescriptors) { | 
|  | 2616 | Record *ClassRecord = Records.getClass(Descriptor.TableGenName); | 
|  | 2617 | AttrClass *Class = new AttrClass(Descriptor, ClassRecord); | 
|  | 2618 | Classes.emplace_back(Class); | 
|  | 2619 | } | 
|  | 2620 |  | 
|  | 2621 | // Link up the hierarchy. | 
|  | 2622 | for (auto &Class : Classes) { | 
|  | 2623 | if (AttrClass *SuperClass = findSuperClass(Class->TheRecord)) { | 
|  | 2624 | Class->SuperClass = SuperClass; | 
|  | 2625 | SuperClass->SubClasses.push_back(Class.get()); | 
|  | 2626 | } | 
|  | 2627 | } | 
|  | 2628 |  | 
|  | 2629 | #ifndef NDEBUG | 
|  | 2630 | for (auto i = Classes.begin(), e = Classes.end(); i != e; ++i) { | 
|  | 2631 | assert((i == Classes.begin()) == ((*i)->SuperClass == nullptr) && | 
|  | 2632 | "only the first class should be a root class!"); | 
|  | 2633 | } | 
|  | 2634 | #endif | 
|  | 2635 | } | 
|  | 2636 |  | 
|  | 2637 | void emitDefaultDefines(raw_ostream &OS) const { | 
|  | 2638 | for (auto &Class : Classes) { | 
|  | 2639 | Class->emitDefaultDefines(OS); | 
|  | 2640 | } | 
|  | 2641 | } | 
|  | 2642 |  | 
|  | 2643 | void emitUndefs(raw_ostream &OS) const { | 
|  | 2644 | for (auto &Class : Classes) { | 
|  | 2645 | Class->emitUndefs(OS); | 
|  | 2646 | } | 
|  | 2647 | } | 
|  | 2648 |  | 
|  | 2649 | void emitAttrLists(raw_ostream &OS) const { | 
|  | 2650 | // Just start from the root class. | 
|  | 2651 | Classes[0]->emitAttrList(OS); | 
|  | 2652 | } | 
|  | 2653 |  | 
|  | 2654 | void emitAttrRanges(raw_ostream &OS) const { | 
|  | 2655 | for (auto &Class : Classes) | 
|  | 2656 | Class->emitAttrRange(OS); | 
|  | 2657 | } | 
|  | 2658 |  | 
|  | 2659 | void classifyAttr(Record *Attr) { | 
|  | 2660 | // Add the attribute to the root class. | 
|  | 2661 | Classes[0]->classifyAttrOnRoot(Attr); | 
|  | 2662 | } | 
|  | 2663 |  | 
|  | 2664 | private: | 
|  | 2665 | AttrClass *findClassByRecord(Record *R) const { | 
|  | 2666 | for (auto &Class : Classes) { | 
|  | 2667 | if (Class->TheRecord == R) | 
|  | 2668 | return Class.get(); | 
|  | 2669 | } | 
|  | 2670 | return nullptr; | 
|  | 2671 | } | 
|  | 2672 |  | 
|  | 2673 | AttrClass *findSuperClass(Record *R) const { | 
|  | 2674 | // TableGen flattens the superclass list, so we just need to walk it | 
|  | 2675 | // in reverse. | 
|  | 2676 | auto SuperClasses = R->getSuperClasses(); | 
|  | 2677 | for (signed i = 0, e = SuperClasses.size(); i != e; ++i) { | 
|  | 2678 | auto SuperClass = findClassByRecord(SuperClasses[e - i - 1].first); | 
|  | 2679 | if (SuperClass) return SuperClass; | 
|  | 2680 | } | 
|  | 2681 | return nullptr; | 
|  | 2682 | } | 
|  | 2683 | }; | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 2684 |  | 
|  | 2685 | } // end anonymous namespace | 
| John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2686 |  | 
| Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 2687 | namespace clang { | 
| Eugene Zelenko | a9f3e90 | 2016-05-12 22:27:08 +0000 | [diff] [blame] | 2688 |  | 
| Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 2689 | // Emits the enumeration list for attributes. | 
|  | 2690 | void EmitClangAttrList(RecordKeeper &Records, raw_ostream &OS) { | 
| Dmitri Gribenko | 6b11fca | 2013-01-30 21:54:20 +0000 | [diff] [blame] | 2691 | emitSourceFileHeader("List of all attributes that Clang recognizes", OS); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2692 |  | 
| John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2693 | AttrClassHierarchy Hierarchy(Records); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2694 |  | 
| John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2695 | // Add defaulting macro definitions. | 
|  | 2696 | Hierarchy.emitDefaultDefines(OS); | 
|  | 2697 | emitDefaultDefine(OS, "PRAGMA_SPELLING_ATTR", nullptr); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2698 |  | 
| John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2699 | std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); | 
|  | 2700 | std::vector<Record *> PragmaAttrs; | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2701 | for (auto *Attr : Attrs) { | 
|  | 2702 | if (!Attr->getValueAsBit("ASTNode")) | 
| Douglas Gregor | b2daf84 | 2012-05-02 15:56:52 +0000 | [diff] [blame] | 2703 | continue; | 
| Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 2704 |  | 
| John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2705 | // Add the attribute to the ad-hoc groups. | 
| Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 2706 | if (AttrHasPragmaSpelling(Attr)) | 
|  | 2707 | PragmaAttrs.push_back(Attr); | 
|  | 2708 |  | 
| John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2709 | // Place it in the hierarchy. | 
|  | 2710 | Hierarchy.classifyAttr(Attr); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2711 | } | 
|  | 2712 |  | 
| John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2713 | // Emit the main attribute list. | 
|  | 2714 | Hierarchy.emitAttrLists(OS); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2715 |  | 
| John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 2716 | // Emit the ad hoc groups. | 
|  | 2717 | emitAttrList(OS, "PRAGMA_SPELLING_ATTR", PragmaAttrs); | 
|  | 2718 |  | 
|  | 2719 | // Emit the attribute ranges. | 
|  | 2720 | OS << "#ifdef ATTR_RANGE\n"; | 
|  | 2721 | Hierarchy.emitAttrRanges(OS); | 
|  | 2722 | OS << "#undef ATTR_RANGE\n"; | 
|  | 2723 | OS << "#endif\n"; | 
|  | 2724 |  | 
|  | 2725 | Hierarchy.emitUndefs(OS); | 
| Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 2726 | OS << "#undef PRAGMA_SPELLING_ATTR\n"; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2727 | } | 
|  | 2728 |  | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 2729 | // Emits the enumeration list for attributes. | 
|  | 2730 | void EmitClangAttrSubjectMatchRuleList(RecordKeeper &Records, raw_ostream &OS) { | 
|  | 2731 | emitSourceFileHeader( | 
|  | 2732 | "List of all attribute subject matching rules that Clang recognizes", OS); | 
|  | 2733 | PragmaClangAttributeSupport &PragmaAttributeSupport = | 
|  | 2734 | getPragmaAttributeSupport(Records); | 
|  | 2735 | emitDefaultDefine(OS, "ATTR_MATCH_RULE", nullptr); | 
|  | 2736 | PragmaAttributeSupport.emitMatchRuleList(OS); | 
|  | 2737 | OS << "#undef ATTR_MATCH_RULE\n"; | 
|  | 2738 | } | 
|  | 2739 |  | 
| Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 2740 | // Emits the code to read an attribute from a precompiled header. | 
|  | 2741 | void EmitClangAttrPCHRead(RecordKeeper &Records, raw_ostream &OS) { | 
| Dmitri Gribenko | 6b11fca | 2013-01-30 21:54:20 +0000 | [diff] [blame] | 2742 | emitSourceFileHeader("Attribute deserialization code", OS); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2743 |  | 
|  | 2744 | Record *InhClass = Records.getClass("InheritableAttr"); | 
|  | 2745 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), | 
|  | 2746 | ArgRecords; | 
| Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2747 | std::vector<std::unique_ptr<Argument>> Args; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2748 |  | 
|  | 2749 | OS << "  switch (Kind) {\n"; | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2750 | for (const auto *Attr : Attrs) { | 
|  | 2751 | const Record &R = *Attr; | 
| Douglas Gregor | b2daf84 | 2012-05-02 15:56:52 +0000 | [diff] [blame] | 2752 | if (!R.getValueAsBit("ASTNode")) | 
|  | 2753 | continue; | 
|  | 2754 |  | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2755 | OS << "  case attr::" << R.getName() << ": {\n"; | 
|  | 2756 | if (R.isSubClassOf(InhClass)) | 
| David L. Jones | 267b884 | 2017-01-24 01:04:30 +0000 | [diff] [blame] | 2757 | OS << "    bool isInherited = Record.readInt();\n"; | 
|  | 2758 | OS << "    bool isImplicit = Record.readInt();\n"; | 
|  | 2759 | OS << "    unsigned Spelling = Record.readInt();\n"; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2760 | ArgRecords = R.getValueAsListOfDefs("Args"); | 
|  | 2761 | Args.clear(); | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2762 | for (const auto *Arg : ArgRecords) { | 
|  | 2763 | Args.emplace_back(createArgument(*Arg, R.getName())); | 
| Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2764 | Args.back()->writePCHReadDecls(OS); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2765 | } | 
|  | 2766 | OS << "    New = new (Context) " << R.getName() << "Attr(Range, Context"; | 
| Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 2767 | for (auto const &ri : Args) { | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2768 | OS << ", "; | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 2769 | ri->writePCHReadArgs(OS); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2770 | } | 
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2771 | OS << ", Spelling);\n"; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2772 | if (R.isSubClassOf(InhClass)) | 
|  | 2773 | OS << "    cast<InheritableAttr>(New)->setInherited(isInherited);\n"; | 
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2774 | OS << "    New->setImplicit(isImplicit);\n"; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2775 | OS << "    break;\n"; | 
|  | 2776 | OS << "  }\n"; | 
|  | 2777 | } | 
|  | 2778 | OS << "  }\n"; | 
|  | 2779 | } | 
|  | 2780 |  | 
| Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 2781 | // Emits the code to write an attribute to a precompiled header. | 
|  | 2782 | void EmitClangAttrPCHWrite(RecordKeeper &Records, raw_ostream &OS) { | 
| Dmitri Gribenko | 6b11fca | 2013-01-30 21:54:20 +0000 | [diff] [blame] | 2783 | emitSourceFileHeader("Attribute serialization code", OS); | 
|  | 2784 |  | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2785 | Record *InhClass = Records.getClass("InheritableAttr"); | 
|  | 2786 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2787 |  | 
|  | 2788 | OS << "  switch (A->getKind()) {\n"; | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2789 | for (const auto *Attr : Attrs) { | 
|  | 2790 | const Record &R = *Attr; | 
| Douglas Gregor | b2daf84 | 2012-05-02 15:56:52 +0000 | [diff] [blame] | 2791 | if (!R.getValueAsBit("ASTNode")) | 
|  | 2792 | continue; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2793 | OS << "  case attr::" << R.getName() << ": {\n"; | 
|  | 2794 | Args = R.getValueAsListOfDefs("Args"); | 
|  | 2795 | if (R.isSubClassOf(InhClass) || !Args.empty()) | 
| Eugene Zelenko | 5f02b77 | 2015-12-08 18:49:01 +0000 | [diff] [blame] | 2796 | OS << "    const auto *SA = cast<" << R.getName() | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2797 | << "Attr>(A);\n"; | 
|  | 2798 | if (R.isSubClassOf(InhClass)) | 
|  | 2799 | OS << "    Record.push_back(SA->isInherited());\n"; | 
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2800 | OS << "    Record.push_back(A->isImplicit());\n"; | 
|  | 2801 | OS << "    Record.push_back(A->getSpellingListIndex());\n"; | 
|  | 2802 |  | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 2803 | for (const auto *Arg : Args) | 
|  | 2804 | createArgument(*Arg, R.getName())->writePCHWrite(OS); | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2805 | OS << "    break;\n"; | 
|  | 2806 | OS << "  }\n"; | 
|  | 2807 | } | 
|  | 2808 | OS << "  }\n"; | 
|  | 2809 | } | 
|  | 2810 |  | 
| Erich Keane | 7544967 | 2017-12-20 18:51:08 +0000 | [diff] [blame] | 2811 | // Helper function for GenerateTargetSpecificAttrChecks that alters the 'Test' | 
|  | 2812 | // parameter with only a single check type, if applicable. | 
| Richard Smith | 78b239e | 2019-06-20 20:44:45 +0000 | [diff] [blame] | 2813 | static bool GenerateTargetSpecificAttrCheck(const Record *R, std::string &Test, | 
| Erich Keane | 7544967 | 2017-12-20 18:51:08 +0000 | [diff] [blame] | 2814 | std::string *FnName, | 
|  | 2815 | StringRef ListName, | 
|  | 2816 | StringRef CheckAgainst, | 
|  | 2817 | StringRef Scope) { | 
|  | 2818 | if (!R->isValueUnset(ListName)) { | 
|  | 2819 | Test += " && ("; | 
|  | 2820 | std::vector<StringRef> Items = R->getValueAsListOfStrings(ListName); | 
|  | 2821 | for (auto I = Items.begin(), E = Items.end(); I != E; ++I) { | 
|  | 2822 | StringRef Part = *I; | 
|  | 2823 | Test += CheckAgainst; | 
|  | 2824 | Test += " == "; | 
|  | 2825 | Test += Scope; | 
|  | 2826 | Test += Part; | 
|  | 2827 | if (I + 1 != E) | 
|  | 2828 | Test += " || "; | 
|  | 2829 | if (FnName) | 
|  | 2830 | *FnName += Part; | 
|  | 2831 | } | 
|  | 2832 | Test += ")"; | 
| Richard Smith | 78b239e | 2019-06-20 20:44:45 +0000 | [diff] [blame] | 2833 | return true; | 
| Erich Keane | 7544967 | 2017-12-20 18:51:08 +0000 | [diff] [blame] | 2834 | } | 
| Richard Smith | 78b239e | 2019-06-20 20:44:45 +0000 | [diff] [blame] | 2835 | return false; | 
| Erich Keane | 7544967 | 2017-12-20 18:51:08 +0000 | [diff] [blame] | 2836 | } | 
|  | 2837 |  | 
| Bob Wilson | 0058b82 | 2015-07-20 22:57:36 +0000 | [diff] [blame] | 2838 | // Generate a conditional expression to check if the current target satisfies | 
|  | 2839 | // the conditions for a TargetSpecificAttr record, and append the code for | 
|  | 2840 | // those checks to the Test string. If the FnName string pointer is non-null, | 
|  | 2841 | // append a unique suffix to distinguish this set of target checks from other | 
|  | 2842 | // TargetSpecificAttr records. | 
| Richard Smith | 78b239e | 2019-06-20 20:44:45 +0000 | [diff] [blame] | 2843 | static bool GenerateTargetSpecificAttrChecks(const Record *R, | 
| Craig Topper | 0064858 | 2017-05-31 19:01:22 +0000 | [diff] [blame] | 2844 | std::vector<StringRef> &Arches, | 
| Bob Wilson | 0058b82 | 2015-07-20 22:57:36 +0000 | [diff] [blame] | 2845 | std::string &Test, | 
|  | 2846 | std::string *FnName) { | 
| Richard Smith | 78b239e | 2019-06-20 20:44:45 +0000 | [diff] [blame] | 2847 | bool AnyTargetChecks = false; | 
|  | 2848 |  | 
| Bob Wilson | 0058b82 | 2015-07-20 22:57:36 +0000 | [diff] [blame] | 2849 | // It is assumed that there will be an llvm::Triple object | 
|  | 2850 | // named "T" and a TargetInfo object named "Target" within | 
|  | 2851 | // scope that can be used to determine whether the attribute exists in | 
|  | 2852 | // a given target. | 
| Erich Keane | 7544967 | 2017-12-20 18:51:08 +0000 | [diff] [blame] | 2853 | Test += "true"; | 
|  | 2854 | // If one or more architectures is specified, check those.  Arches are handled | 
|  | 2855 | // differently because GenerateTargetRequirements needs to combine the list | 
|  | 2856 | // with ParseKind. | 
|  | 2857 | if (!Arches.empty()) { | 
| Richard Smith | 78b239e | 2019-06-20 20:44:45 +0000 | [diff] [blame] | 2858 | AnyTargetChecks = true; | 
| Erich Keane | 7544967 | 2017-12-20 18:51:08 +0000 | [diff] [blame] | 2859 | Test += " && ("; | 
|  | 2860 | for (auto I = Arches.begin(), E = Arches.end(); I != E; ++I) { | 
|  | 2861 | StringRef Part = *I; | 
|  | 2862 | Test += "T.getArch() == llvm::Triple::"; | 
|  | 2863 | Test += Part; | 
|  | 2864 | if (I + 1 != E) | 
|  | 2865 | Test += " || "; | 
|  | 2866 | if (FnName) | 
|  | 2867 | *FnName += Part; | 
|  | 2868 | } | 
|  | 2869 | Test += ")"; | 
| Bob Wilson | 0058b82 | 2015-07-20 22:57:36 +0000 | [diff] [blame] | 2870 | } | 
| Bob Wilson | 0058b82 | 2015-07-20 22:57:36 +0000 | [diff] [blame] | 2871 |  | 
|  | 2872 | // If the attribute is specific to particular OSes, check those. | 
| Richard Smith | 78b239e | 2019-06-20 20:44:45 +0000 | [diff] [blame] | 2873 | AnyTargetChecks |= GenerateTargetSpecificAttrCheck( | 
|  | 2874 | R, Test, FnName, "OSes", "T.getOS()", "llvm::Triple::"); | 
| Bob Wilson | 0058b82 | 2015-07-20 22:57:36 +0000 | [diff] [blame] | 2875 |  | 
| Erich Keane | 7544967 | 2017-12-20 18:51:08 +0000 | [diff] [blame] | 2876 | // If one or more object formats is specified, check those. | 
| Richard Smith | 78b239e | 2019-06-20 20:44:45 +0000 | [diff] [blame] | 2877 | AnyTargetChecks |= | 
|  | 2878 | GenerateTargetSpecificAttrCheck(R, Test, FnName, "ObjectFormats", | 
|  | 2879 | "T.getObjectFormat()", "llvm::Triple::"); | 
|  | 2880 |  | 
|  | 2881 | // If custom code is specified, emit it. | 
|  | 2882 | StringRef Code = R->getValueAsString("CustomCode"); | 
|  | 2883 | if (!Code.empty()) { | 
|  | 2884 | AnyTargetChecks = true; | 
|  | 2885 | Test += " && ("; | 
|  | 2886 | Test += Code; | 
|  | 2887 | Test += ")"; | 
|  | 2888 | } | 
|  | 2889 |  | 
|  | 2890 | return AnyTargetChecks; | 
| Bob Wilson | 0058b82 | 2015-07-20 22:57:36 +0000 | [diff] [blame] | 2891 | } | 
|  | 2892 |  | 
| Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2893 | static void GenerateHasAttrSpellingStringSwitch( | 
|  | 2894 | const std::vector<Record *> &Attrs, raw_ostream &OS, | 
|  | 2895 | const std::string &Variety = "", const std::string &Scope = "") { | 
|  | 2896 | for (const auto *Attr : Attrs) { | 
| Aaron Ballman | a0344c5 | 2014-11-14 13:44:02 +0000 | [diff] [blame] | 2897 | // C++11-style attributes have specific version information associated with | 
|  | 2898 | // them. If the attribute has no scope, the version information must not | 
|  | 2899 | // have the default value (1), as that's incorrect. Instead, the unscoped | 
|  | 2900 | // attribute version information should be taken from the SD-6 standing | 
|  | 2901 | // document, which can be found at: | 
|  | 2902 | // https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations | 
|  | 2903 | int Version = 1; | 
|  | 2904 |  | 
|  | 2905 | if (Variety == "CXX11") { | 
|  | 2906 | std::vector<Record *> Spellings = Attr->getValueAsListOfDefs("Spellings"); | 
|  | 2907 | for (const auto &Spelling : Spellings) { | 
|  | 2908 | if (Spelling->getValueAsString("Variety") == "CXX11") { | 
|  | 2909 | Version = static_cast<int>(Spelling->getValueAsInt("Version")); | 
|  | 2910 | if (Scope.empty() && Version == 1) | 
|  | 2911 | PrintError(Spelling->getLoc(), "C++ standard attributes must " | 
|  | 2912 | "have valid version information."); | 
|  | 2913 | break; | 
|  | 2914 | } | 
|  | 2915 | } | 
|  | 2916 | } | 
|  | 2917 |  | 
| Aaron Ballman | 0fa06d8 | 2014-01-09 22:57:44 +0000 | [diff] [blame] | 2918 | std::string Test; | 
| Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2919 | if (Attr->isSubClassOf("TargetSpecificAttr")) { | 
|  | 2920 | const Record *R = Attr->getValueAsDef("Target"); | 
| Craig Topper | 0064858 | 2017-05-31 19:01:22 +0000 | [diff] [blame] | 2921 | std::vector<StringRef> Arches = R->getValueAsListOfStrings("Arches"); | 
| Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 2922 | GenerateTargetSpecificAttrChecks(R, Arches, Test, nullptr); | 
| Bob Wilson | 7c73083 | 2015-07-20 22:57:31 +0000 | [diff] [blame] | 2923 |  | 
| Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2924 | // If this is the C++11 variety, also add in the LangOpts test. | 
|  | 2925 | if (Variety == "CXX11") | 
|  | 2926 | Test += " && LangOpts.CPlusPlus11"; | 
| Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 2927 | else if (Variety == "C2x") | 
|  | 2928 | Test += " && LangOpts.DoubleSquareBracketAttributes"; | 
| Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2929 | } else if (Variety == "CXX11") | 
|  | 2930 | // C++11 mode should be checked against LangOpts, which is presumed to be | 
|  | 2931 | // present in the caller. | 
|  | 2932 | Test = "LangOpts.CPlusPlus11"; | 
| Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 2933 | else if (Variety == "C2x") | 
|  | 2934 | Test = "LangOpts.DoubleSquareBracketAttributes"; | 
| Aaron Ballman | 0fa06d8 | 2014-01-09 22:57:44 +0000 | [diff] [blame] | 2935 |  | 
| Aaron Ballman | a0344c5 | 2014-11-14 13:44:02 +0000 | [diff] [blame] | 2936 | std::string TestStr = | 
| Aaron Ballman | 28afa18 | 2014-11-17 18:17:19 +0000 | [diff] [blame] | 2937 | !Test.empty() ? Test + " ? " + llvm::itostr(Version) + " : 0" : "1"; | 
| Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2938 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr); | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 2939 | for (const auto &S : Spellings) | 
| Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2940 | if (Variety.empty() || (Variety == S.variety() && | 
|  | 2941 | (Scope.empty() || Scope == S.nameSpace()))) | 
| Aaron Ballman | a0344c5 | 2014-11-14 13:44:02 +0000 | [diff] [blame] | 2942 | OS << "    .Case(\"" << S.name() << "\", " << TestStr << ")\n"; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2943 | } | 
| Aaron Ballman | a0344c5 | 2014-11-14 13:44:02 +0000 | [diff] [blame] | 2944 | OS << "    .Default(0);\n"; | 
| Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2945 | } | 
|  | 2946 |  | 
|  | 2947 | // Emits the list of spellings for attributes. | 
|  | 2948 | void EmitClangAttrHasAttrImpl(RecordKeeper &Records, raw_ostream &OS) { | 
|  | 2949 | emitSourceFileHeader("Code to implement the __has_attribute logic", OS); | 
|  | 2950 |  | 
|  | 2951 | // Separate all of the attributes out into four group: generic, C++11, GNU, | 
|  | 2952 | // and declspecs. Then generate a big switch statement for each of them. | 
|  | 2953 | std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); | 
| Nico Weber | 20e0804 | 2016-09-03 02:55:10 +0000 | [diff] [blame] | 2954 | std::vector<Record *> Declspec, Microsoft, GNU, Pragma; | 
| Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 2955 | std::map<std::string, std::vector<Record *>> CXX, C2x; | 
| Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2956 |  | 
|  | 2957 | // Walk over the list of all attributes, and split them out based on the | 
|  | 2958 | // spelling variety. | 
|  | 2959 | for (auto *R : Attrs) { | 
|  | 2960 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R); | 
|  | 2961 | for (const auto &SI : Spellings) { | 
| Benjamin Kramer | 2e018ef | 2016-05-27 13:36:58 +0000 | [diff] [blame] | 2962 | const std::string &Variety = SI.variety(); | 
| Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2963 | if (Variety == "GNU") | 
|  | 2964 | GNU.push_back(R); | 
|  | 2965 | else if (Variety == "Declspec") | 
|  | 2966 | Declspec.push_back(R); | 
| Nico Weber | 20e0804 | 2016-09-03 02:55:10 +0000 | [diff] [blame] | 2967 | else if (Variety == "Microsoft") | 
|  | 2968 | Microsoft.push_back(R); | 
| Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 2969 | else if (Variety == "CXX11") | 
| Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2970 | CXX[SI.nameSpace()].push_back(R); | 
| Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 2971 | else if (Variety == "C2x") | 
|  | 2972 | C2x[SI.nameSpace()].push_back(R); | 
| Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 2973 | else if (Variety == "Pragma") | 
|  | 2974 | Pragma.push_back(R); | 
| Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2975 | } | 
|  | 2976 | } | 
|  | 2977 |  | 
| Bob Wilson | 7c73083 | 2015-07-20 22:57:31 +0000 | [diff] [blame] | 2978 | OS << "const llvm::Triple &T = Target.getTriple();\n"; | 
| Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2979 | OS << "switch (Syntax) {\n"; | 
| Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2980 | OS << "case AttrSyntax::GNU:\n"; | 
| Aaron Ballman | a0344c5 | 2014-11-14 13:44:02 +0000 | [diff] [blame] | 2981 | OS << "  return llvm::StringSwitch<int>(Name)\n"; | 
| Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2982 | GenerateHasAttrSpellingStringSwitch(GNU, OS, "GNU"); | 
|  | 2983 | OS << "case AttrSyntax::Declspec:\n"; | 
| Aaron Ballman | a0344c5 | 2014-11-14 13:44:02 +0000 | [diff] [blame] | 2984 | OS << "  return llvm::StringSwitch<int>(Name)\n"; | 
| Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 2985 | GenerateHasAttrSpellingStringSwitch(Declspec, OS, "Declspec"); | 
| Nico Weber | 20e0804 | 2016-09-03 02:55:10 +0000 | [diff] [blame] | 2986 | OS << "case AttrSyntax::Microsoft:\n"; | 
|  | 2987 | OS << "  return llvm::StringSwitch<int>(Name)\n"; | 
|  | 2988 | GenerateHasAttrSpellingStringSwitch(Microsoft, OS, "Microsoft"); | 
| Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 2989 | OS << "case AttrSyntax::Pragma:\n"; | 
| Aaron Ballman | a0344c5 | 2014-11-14 13:44:02 +0000 | [diff] [blame] | 2990 | OS << "  return llvm::StringSwitch<int>(Name)\n"; | 
| Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 2991 | GenerateHasAttrSpellingStringSwitch(Pragma, OS, "Pragma"); | 
| Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 2992 | auto fn = [&OS](const char *Spelling, const char *Variety, | 
|  | 2993 | const std::map<std::string, std::vector<Record *>> &List) { | 
|  | 2994 | OS << "case AttrSyntax::" << Variety << ": {\n"; | 
|  | 2995 | // C++11-style attributes are further split out based on the Scope. | 
|  | 2996 | for (auto I = List.cbegin(), E = List.cend(); I != E; ++I) { | 
| Stephen Kelly | db8fac1 | 2019-01-11 19:16:01 +0000 | [diff] [blame] | 2997 | if (I != List.cbegin()) | 
|  | 2998 | OS << " else "; | 
|  | 2999 | if (I->first.empty()) | 
|  | 3000 | OS << "if (ScopeName == \"\") {\n"; | 
|  | 3001 | else | 
|  | 3002 | OS << "if (ScopeName == \"" << I->first << "\") {\n"; | 
|  | 3003 | OS << "  return llvm::StringSwitch<int>(Name)\n"; | 
|  | 3004 | GenerateHasAttrSpellingStringSwitch(I->second, OS, Spelling, I->first); | 
|  | 3005 | OS << "}"; | 
| Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 3006 | } | 
| Aaron Ballman | 4ff3b5ab | 2017-10-18 12:11:58 +0000 | [diff] [blame] | 3007 | OS << "\n} break;\n"; | 
| Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 3008 | }; | 
|  | 3009 | fn("CXX11", "CXX", CXX); | 
|  | 3010 | fn("C2x", "C", C2x); | 
| Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 3011 | OS << "}\n"; | 
| Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 3012 | } | 
|  | 3013 |  | 
| Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3014 | void EmitClangAttrSpellingListIndex(RecordKeeper &Records, raw_ostream &OS) { | 
| Dmitri Gribenko | 6b11fca | 2013-01-30 21:54:20 +0000 | [diff] [blame] | 3015 | emitSourceFileHeader("Code to translate different attribute spellings " | 
|  | 3016 | "into internal identifiers", OS); | 
| Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3017 |  | 
| John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 3018 | OS << "  switch (AttrKind) {\n"; | 
| Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3019 |  | 
| Aaron Ballman | 64e6986 | 2013-12-15 13:05:48 +0000 | [diff] [blame] | 3020 | ParsedAttrMap Attrs = getParsedAttrList(Records); | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3021 | for (const auto &I : Attrs) { | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 3022 | const Record &R = *I.second; | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 3023 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R); | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3024 | OS << "  case AT_" << I.first << ": {\n"; | 
| Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 3025 | for (unsigned I = 0; I < Spellings.size(); ++ I) { | 
| Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 3026 | OS << "    if (Name == \"" << Spellings[I].name() << "\" && " | 
|  | 3027 | << "SyntaxUsed == " | 
|  | 3028 | << StringSwitch<unsigned>(Spellings[I].variety()) | 
|  | 3029 | .Case("GNU", 0) | 
|  | 3030 | .Case("CXX11", 1) | 
| Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 3031 | .Case("C2x", 2) | 
|  | 3032 | .Case("Declspec", 3) | 
|  | 3033 | .Case("Microsoft", 4) | 
|  | 3034 | .Case("Keyword", 5) | 
|  | 3035 | .Case("Pragma", 6) | 
| Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 3036 | .Default(0) | 
|  | 3037 | << " && Scope == \"" << Spellings[I].nameSpace() << "\")\n" | 
|  | 3038 | << "        return " << I << ";\n"; | 
| Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3039 | } | 
| Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 3040 |  | 
|  | 3041 | OS << "    break;\n"; | 
|  | 3042 | OS << "  }\n"; | 
| Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3043 | } | 
|  | 3044 |  | 
|  | 3045 | OS << "  }\n"; | 
| Aaron Ballman | 64e6986 | 2013-12-15 13:05:48 +0000 | [diff] [blame] | 3046 | OS << "  return 0;\n"; | 
| Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3047 | } | 
|  | 3048 |  | 
| DeLesley Hutchins | c4a8243 | 2013-12-30 17:24:36 +0000 | [diff] [blame] | 3049 | // Emits code used by RecursiveASTVisitor to visit attributes | 
|  | 3050 | void EmitClangAttrASTVisitor(RecordKeeper &Records, raw_ostream &OS) { | 
|  | 3051 | emitSourceFileHeader("Used by RecursiveASTVisitor to visit attributes.", OS); | 
|  | 3052 |  | 
|  | 3053 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"); | 
|  | 3054 |  | 
|  | 3055 | // Write method declarations for Traverse* methods. | 
|  | 3056 | // We emit this here because we only generate methods for attributes that | 
|  | 3057 | // are declared as ASTNodes. | 
|  | 3058 | OS << "#ifdef ATTR_VISITOR_DECLS_ONLY\n\n"; | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 3059 | for (const auto *Attr : Attrs) { | 
|  | 3060 | const Record &R = *Attr; | 
| DeLesley Hutchins | c4a8243 | 2013-12-30 17:24:36 +0000 | [diff] [blame] | 3061 | if (!R.getValueAsBit("ASTNode")) | 
|  | 3062 | continue; | 
|  | 3063 | OS << "  bool Traverse" | 
|  | 3064 | << R.getName() << "Attr(" << R.getName() << "Attr *A);\n"; | 
|  | 3065 | OS << "  bool Visit" | 
|  | 3066 | << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n" | 
|  | 3067 | << "    return true; \n" | 
| Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 3068 | << "  }\n"; | 
| DeLesley Hutchins | c4a8243 | 2013-12-30 17:24:36 +0000 | [diff] [blame] | 3069 | } | 
|  | 3070 | OS << "\n#else // ATTR_VISITOR_DECLS_ONLY\n\n"; | 
|  | 3071 |  | 
|  | 3072 | // Write individual Traverse* methods for each attribute class. | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 3073 | for (const auto *Attr : Attrs) { | 
|  | 3074 | const Record &R = *Attr; | 
| DeLesley Hutchins | c4a8243 | 2013-12-30 17:24:36 +0000 | [diff] [blame] | 3075 | if (!R.getValueAsBit("ASTNode")) | 
|  | 3076 | continue; | 
|  | 3077 |  | 
|  | 3078 | OS << "template <typename Derived>\n" | 
| DeLesley Hutchins | bb79c33 | 2013-12-30 21:03:02 +0000 | [diff] [blame] | 3079 | << "bool VISITORCLASS<Derived>::Traverse" | 
| DeLesley Hutchins | c4a8243 | 2013-12-30 17:24:36 +0000 | [diff] [blame] | 3080 | << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n" | 
|  | 3081 | << "  if (!getDerived().VisitAttr(A))\n" | 
|  | 3082 | << "    return false;\n" | 
|  | 3083 | << "  if (!getDerived().Visit" << R.getName() << "Attr(A))\n" | 
|  | 3084 | << "    return false;\n"; | 
|  | 3085 |  | 
|  | 3086 | std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args"); | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 3087 | for (const auto *Arg : ArgRecords) | 
|  | 3088 | createArgument(*Arg, R.getName())->writeASTVisitorTraversal(OS); | 
| DeLesley Hutchins | c4a8243 | 2013-12-30 17:24:36 +0000 | [diff] [blame] | 3089 |  | 
|  | 3090 | OS << "  return true;\n"; | 
|  | 3091 | OS << "}\n\n"; | 
|  | 3092 | } | 
|  | 3093 |  | 
|  | 3094 | // Write generic Traverse routine | 
|  | 3095 | OS << "template <typename Derived>\n" | 
| DeLesley Hutchins | bb79c33 | 2013-12-30 21:03:02 +0000 | [diff] [blame] | 3096 | << "bool VISITORCLASS<Derived>::TraverseAttr(Attr *A) {\n" | 
| DeLesley Hutchins | c4a8243 | 2013-12-30 17:24:36 +0000 | [diff] [blame] | 3097 | << "  if (!A)\n" | 
|  | 3098 | << "    return true;\n" | 
|  | 3099 | << "\n" | 
| John McCall | 2225c8b | 2016-03-01 00:18:05 +0000 | [diff] [blame] | 3100 | << "  switch (A->getKind()) {\n"; | 
| DeLesley Hutchins | c4a8243 | 2013-12-30 17:24:36 +0000 | [diff] [blame] | 3101 |  | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 3102 | for (const auto *Attr : Attrs) { | 
|  | 3103 | const Record &R = *Attr; | 
| DeLesley Hutchins | c4a8243 | 2013-12-30 17:24:36 +0000 | [diff] [blame] | 3104 | if (!R.getValueAsBit("ASTNode")) | 
|  | 3105 | continue; | 
|  | 3106 |  | 
|  | 3107 | OS << "    case attr::" << R.getName() << ":\n" | 
|  | 3108 | << "      return getDerived().Traverse" << R.getName() << "Attr(" | 
|  | 3109 | << "cast<" << R.getName() << "Attr>(A));\n"; | 
|  | 3110 | } | 
| John McCall | 5d7cf77 | 2016-03-01 02:09:20 +0000 | [diff] [blame] | 3111 | OS << "  }\n";  // end switch | 
|  | 3112 | OS << "  llvm_unreachable(\"bad attribute kind\");\n"; | 
| DeLesley Hutchins | c4a8243 | 2013-12-30 17:24:36 +0000 | [diff] [blame] | 3113 | OS << "}\n";  // end function | 
|  | 3114 | OS << "#endif  // ATTR_VISITOR_DECLS_ONLY\n"; | 
|  | 3115 | } | 
|  | 3116 |  | 
| Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 3117 | void EmitClangAttrTemplateInstantiateHelper(const std::vector<Record *> &Attrs, | 
|  | 3118 | raw_ostream &OS, | 
|  | 3119 | bool AppliesToDecl) { | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 3120 |  | 
| Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 3121 | OS << "  switch (At->getKind()) {\n"; | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 3122 | for (const auto *Attr : Attrs) { | 
|  | 3123 | const Record &R = *Attr; | 
| Douglas Gregor | b2daf84 | 2012-05-02 15:56:52 +0000 | [diff] [blame] | 3124 | if (!R.getValueAsBit("ASTNode")) | 
|  | 3125 | continue; | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 3126 | OS << "    case attr::" << R.getName() << ": {\n"; | 
| Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 3127 | bool ShouldClone = R.getValueAsBit("Clone") && | 
|  | 3128 | (!AppliesToDecl || | 
|  | 3129 | R.getValueAsBit("MeaningfulToClassTemplateDefinition")); | 
| Rafael Espindola | 7f90b7d | 2012-05-15 14:09:55 +0000 | [diff] [blame] | 3130 |  | 
|  | 3131 | if (!ShouldClone) { | 
| Hans Wennborg | 59dbe86 | 2015-09-29 20:56:43 +0000 | [diff] [blame] | 3132 | OS << "      return nullptr;\n"; | 
| Rafael Espindola | 7f90b7d | 2012-05-15 14:09:55 +0000 | [diff] [blame] | 3133 | OS << "    }\n"; | 
|  | 3134 | continue; | 
|  | 3135 | } | 
|  | 3136 |  | 
| Eugene Zelenko | 5f02b77 | 2015-12-08 18:49:01 +0000 | [diff] [blame] | 3137 | OS << "      const auto *A = cast<" | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 3138 | << R.getName() << "Attr>(At);\n"; | 
|  | 3139 | bool TDependent = R.getValueAsBit("TemplateDependent"); | 
|  | 3140 |  | 
|  | 3141 | if (!TDependent) { | 
|  | 3142 | OS << "      return A->clone(C);\n"; | 
|  | 3143 | OS << "    }\n"; | 
|  | 3144 | continue; | 
|  | 3145 | } | 
|  | 3146 |  | 
|  | 3147 | std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args"); | 
| Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 3148 | std::vector<std::unique_ptr<Argument>> Args; | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 3149 | Args.reserve(ArgRecords.size()); | 
|  | 3150 |  | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 3151 | for (const auto *ArgRecord : ArgRecords) | 
| Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 3152 | Args.emplace_back(createArgument(*ArgRecord, R.getName())); | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 3153 |  | 
| Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 3154 | for (auto const &ai : Args) | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3155 | ai->writeTemplateInstantiation(OS); | 
| Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 3156 |  | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 3157 | OS << "      return new (C) " << R.getName() << "Attr(A->getLocation(), C"; | 
| Aaron Ballman | 8f1439b | 2014-03-05 16:49:55 +0000 | [diff] [blame] | 3158 | for (auto const &ai : Args) { | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 3159 | OS << ", "; | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3160 | ai->writeTemplateInstantiationArgs(OS); | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 3161 | } | 
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 3162 | OS << ", A->getSpellingListIndex());\n    }\n"; | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 3163 | } | 
|  | 3164 | OS << "  } // end switch\n" | 
|  | 3165 | << "  llvm_unreachable(\"Unknown attribute!\");\n" | 
| Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 3166 | << "  return nullptr;\n"; | 
|  | 3167 | } | 
|  | 3168 |  | 
|  | 3169 | // Emits code to instantiate dependent attributes on templates. | 
|  | 3170 | void EmitClangAttrTemplateInstantiate(RecordKeeper &Records, raw_ostream &OS) { | 
|  | 3171 | emitSourceFileHeader("Template instantiation code for attributes", OS); | 
|  | 3172 |  | 
|  | 3173 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"); | 
|  | 3174 |  | 
|  | 3175 | OS << "namespace clang {\n" | 
|  | 3176 | << "namespace sema {\n\n" | 
|  | 3177 | << "Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, " | 
|  | 3178 | << "Sema &S,\n" | 
|  | 3179 | << "        const MultiLevelTemplateArgumentList &TemplateArgs) {\n"; | 
|  | 3180 | EmitClangAttrTemplateInstantiateHelper(Attrs, OS, /*AppliesToDecl*/false); | 
|  | 3181 | OS << "}\n\n" | 
|  | 3182 | << "Attr *instantiateTemplateAttributeForDecl(const Attr *At,\n" | 
|  | 3183 | << " ASTContext &C, Sema &S,\n" | 
|  | 3184 | << "        const MultiLevelTemplateArgumentList &TemplateArgs) {\n"; | 
|  | 3185 | EmitClangAttrTemplateInstantiateHelper(Attrs, OS, /*AppliesToDecl*/true); | 
|  | 3186 | OS << "}\n\n" | 
| Benjamin Kramer | bf8da9d | 2012-02-06 11:13:08 +0000 | [diff] [blame] | 3187 | << "} // end namespace sema\n" | 
|  | 3188 | << "} // end namespace clang\n"; | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 3189 | } | 
|  | 3190 |  | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3191 | // Emits the list of parsed attributes. | 
|  | 3192 | void EmitClangAttrParsedAttrList(RecordKeeper &Records, raw_ostream &OS) { | 
|  | 3193 | emitSourceFileHeader("List of all attributes that Clang recognizes", OS); | 
|  | 3194 |  | 
|  | 3195 | OS << "#ifndef PARSED_ATTR\n"; | 
|  | 3196 | OS << "#define PARSED_ATTR(NAME) NAME\n"; | 
|  | 3197 | OS << "#endif\n\n"; | 
|  | 3198 |  | 
|  | 3199 | ParsedAttrMap Names = getParsedAttrList(Records); | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3200 | for (const auto &I : Names) { | 
|  | 3201 | OS << "PARSED_ATTR(" << I.first << ")\n"; | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3202 | } | 
|  | 3203 | } | 
|  | 3204 |  | 
| Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 3205 | static bool isArgVariadic(const Record &R, StringRef AttrName) { | 
|  | 3206 | return createArgument(R, AttrName)->isVariadic(); | 
|  | 3207 | } | 
|  | 3208 |  | 
| Erich Keane | df9e8ae | 2017-10-16 22:47:26 +0000 | [diff] [blame] | 3209 | static void emitArgInfo(const Record &R, raw_ostream &OS) { | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3210 | // This function will count the number of arguments specified for the | 
|  | 3211 | // attribute and emit the number of required arguments followed by the | 
|  | 3212 | // number of optional arguments. | 
|  | 3213 | std::vector<Record *> Args = R.getValueAsListOfDefs("Args"); | 
|  | 3214 | unsigned ArgCount = 0, OptCount = 0; | 
| Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 3215 | bool HasVariadic = false; | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 3216 | for (const auto *Arg : Args) { | 
| George Burgess IV | 8a36ace | 2016-12-01 17:52:39 +0000 | [diff] [blame] | 3217 | // If the arg is fake, it's the user's job to supply it: general parsing | 
|  | 3218 | // logic shouldn't need to know anything about it. | 
|  | 3219 | if (Arg->getValueAsBit("Fake")) | 
|  | 3220 | continue; | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3221 | Arg->getValueAsBit("Optional") ? ++OptCount : ++ArgCount; | 
| Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 3222 | if (!HasVariadic && isArgVariadic(*Arg, R.getName())) | 
|  | 3223 | HasVariadic = true; | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3224 | } | 
| Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 3225 |  | 
|  | 3226 | // If there is a variadic argument, we will set the optional argument count | 
|  | 3227 | // to its largest value. Since it's currently a 4-bit number, we set it to 15. | 
|  | 3228 | OS << ArgCount << ", " << (HasVariadic ? 15 : OptCount); | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3229 | } | 
|  | 3230 |  | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3231 | static void GenerateDefaultAppertainsTo(raw_ostream &OS) { | 
| Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3232 | OS << "static bool defaultAppertainsTo(Sema &, const ParsedAttr &,"; | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3233 | OS << "const Decl *) {\n"; | 
|  | 3234 | OS << "  return true;\n"; | 
|  | 3235 | OS << "}\n\n"; | 
|  | 3236 | } | 
|  | 3237 |  | 
| Aaron Ballman | adf66b6 | 2017-11-26 20:01:12 +0000 | [diff] [blame] | 3238 | static std::string GetDiagnosticSpelling(const Record &R) { | 
|  | 3239 | std::string Ret = R.getValueAsString("DiagSpelling"); | 
|  | 3240 | if (!Ret.empty()) | 
|  | 3241 | return Ret; | 
|  | 3242 |  | 
|  | 3243 | // If we couldn't find the DiagSpelling in this object, we can check to see | 
|  | 3244 | // if the object is one that has a base, and if it is, loop up to the Base | 
|  | 3245 | // member recursively. | 
|  | 3246 | std::string Super = R.getSuperClasses().back().first->getName(); | 
|  | 3247 | if (Super == "DDecl" || Super == "DStmt") | 
|  | 3248 | return GetDiagnosticSpelling(*R.getValueAsDef("Base")); | 
|  | 3249 |  | 
|  | 3250 | return ""; | 
|  | 3251 | } | 
|  | 3252 |  | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3253 | static std::string CalculateDiagnostic(const Record &S) { | 
|  | 3254 | // If the SubjectList object has a custom diagnostic associated with it, | 
|  | 3255 | // return that directly. | 
| Erich Keane | 3bff414 | 2017-10-16 23:25:24 +0000 | [diff] [blame] | 3256 | const StringRef CustomDiag = S.getValueAsString("CustomDiag"); | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3257 | if (!CustomDiag.empty()) | 
| Aaron Ballman | adf66b6 | 2017-11-26 20:01:12 +0000 | [diff] [blame] | 3258 | return ("\"" + Twine(CustomDiag) + "\"").str(); | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3259 |  | 
| Aaron Ballman | adf66b6 | 2017-11-26 20:01:12 +0000 | [diff] [blame] | 3260 | std::vector<std::string> DiagList; | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3261 | std::vector<Record *> Subjects = S.getValueAsListOfDefs("Subjects"); | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 3262 | for (const auto *Subject : Subjects) { | 
|  | 3263 | const Record &R = *Subject; | 
| Aaron Ballman | adf66b6 | 2017-11-26 20:01:12 +0000 | [diff] [blame] | 3264 | // Get the diagnostic text from the Decl or Stmt node given. | 
|  | 3265 | std::string V = GetDiagnosticSpelling(R); | 
|  | 3266 | if (V.empty()) { | 
|  | 3267 | PrintError(R.getLoc(), | 
|  | 3268 | "Could not determine diagnostic spelling for the node: " + | 
|  | 3269 | R.getName() + "; please add one to DeclNodes.td"); | 
|  | 3270 | } else { | 
|  | 3271 | // The node may contain a list of elements itself, so split the elements | 
|  | 3272 | // by a comma, and trim any whitespace. | 
|  | 3273 | SmallVector<StringRef, 2> Frags; | 
|  | 3274 | llvm::SplitString(V, Frags, ","); | 
|  | 3275 | for (auto Str : Frags) { | 
|  | 3276 | DiagList.push_back(Str.trim()); | 
|  | 3277 | } | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3278 | } | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3279 | } | 
|  | 3280 |  | 
| Aaron Ballman | adf66b6 | 2017-11-26 20:01:12 +0000 | [diff] [blame] | 3281 | if (DiagList.empty()) { | 
|  | 3282 | PrintFatalError(S.getLoc(), | 
|  | 3283 | "Could not deduce diagnostic argument for Attr subjects"); | 
|  | 3284 | return ""; | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3285 | } | 
|  | 3286 |  | 
| Aaron Ballman | adf66b6 | 2017-11-26 20:01:12 +0000 | [diff] [blame] | 3287 | // FIXME: this is not particularly good for localization purposes and ideally | 
|  | 3288 | // should be part of the diagnostics engine itself with some sort of list | 
|  | 3289 | // specifier. | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3290 |  | 
| Aaron Ballman | adf66b6 | 2017-11-26 20:01:12 +0000 | [diff] [blame] | 3291 | // A single member of the list can be returned directly. | 
|  | 3292 | if (DiagList.size() == 1) | 
|  | 3293 | return '"' + DiagList.front() + '"'; | 
|  | 3294 |  | 
|  | 3295 | if (DiagList.size() == 2) | 
|  | 3296 | return '"' + DiagList[0] + " and " + DiagList[1] + '"'; | 
|  | 3297 |  | 
|  | 3298 | // If there are more than two in the list, we serialize the first N - 1 | 
|  | 3299 | // elements with a comma. This leaves the string in the state: foo, bar, | 
|  | 3300 | // baz (but misses quux). We can then add ", and " for the last element | 
|  | 3301 | // manually. | 
|  | 3302 | std::string Diag = llvm::join(DiagList.begin(), DiagList.end() - 1, ", "); | 
|  | 3303 | return '"' + Diag + ", and " + *(DiagList.end() - 1) + '"'; | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3304 | } | 
|  | 3305 |  | 
| Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 3306 | static std::string GetSubjectWithSuffix(const Record *R) { | 
| George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 3307 | const std::string &B = R->getName(); | 
| Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 3308 | if (B == "DeclBase") | 
|  | 3309 | return "Decl"; | 
|  | 3310 | return B + "Decl"; | 
|  | 3311 | } | 
| Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 3312 |  | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3313 | static std::string functionNameForCustomAppertainsTo(const Record &Subject) { | 
|  | 3314 | return "is" + Subject.getName().str(); | 
|  | 3315 | } | 
|  | 3316 |  | 
| Aaron Ballman | 8046903 | 2013-11-29 14:57:58 +0000 | [diff] [blame] | 3317 | static std::string GenerateCustomAppertainsTo(const Record &Subject, | 
|  | 3318 | raw_ostream &OS) { | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3319 | std::string FnName = functionNameForCustomAppertainsTo(Subject); | 
| Aaron Ballman | a358c90 | 2013-12-02 14:58:17 +0000 | [diff] [blame] | 3320 |  | 
| Aaron Ballman | 8046903 | 2013-11-29 14:57:58 +0000 | [diff] [blame] | 3321 | // If this code has already been generated, simply return the previous | 
|  | 3322 | // instance of it. | 
|  | 3323 | static std::set<std::string> CustomSubjectSet; | 
| Eugene Zelenko | 5f02b77 | 2015-12-08 18:49:01 +0000 | [diff] [blame] | 3324 | auto I = CustomSubjectSet.find(FnName); | 
| Aaron Ballman | 8046903 | 2013-11-29 14:57:58 +0000 | [diff] [blame] | 3325 | if (I != CustomSubjectSet.end()) | 
|  | 3326 | return *I; | 
|  | 3327 |  | 
|  | 3328 | Record *Base = Subject.getValueAsDef("Base"); | 
|  | 3329 |  | 
|  | 3330 | // Not currently support custom subjects within custom subjects. | 
|  | 3331 | if (Base->isSubClassOf("SubsetSubject")) { | 
|  | 3332 | PrintFatalError(Subject.getLoc(), | 
|  | 3333 | "SubsetSubjects within SubsetSubjects is not supported"); | 
|  | 3334 | return ""; | 
|  | 3335 | } | 
|  | 3336 |  | 
| Aaron Ballman | 8046903 | 2013-11-29 14:57:58 +0000 | [diff] [blame] | 3337 | OS << "static bool " << FnName << "(const Decl *D) {\n"; | 
| Erich Keane | 873de98 | 2018-08-03 14:24:34 +0000 | [diff] [blame] | 3338 | OS << "  if (const auto *S = dyn_cast<"; | 
|  | 3339 | OS << GetSubjectWithSuffix(Base); | 
|  | 3340 | OS << ">(D))\n"; | 
|  | 3341 | OS << "    return " << Subject.getValueAsString("CheckCode") << ";\n"; | 
| Aaron Ballman | 4755304 | 2014-01-16 14:32:03 +0000 | [diff] [blame] | 3342 | OS << "  return false;\n"; | 
| Aaron Ballman | 8046903 | 2013-11-29 14:57:58 +0000 | [diff] [blame] | 3343 | OS << "}\n\n"; | 
|  | 3344 |  | 
|  | 3345 | CustomSubjectSet.insert(FnName); | 
|  | 3346 | return FnName; | 
|  | 3347 | } | 
|  | 3348 |  | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3349 | static std::string GenerateAppertainsTo(const Record &Attr, raw_ostream &OS) { | 
|  | 3350 | // If the attribute does not contain a Subjects definition, then use the | 
|  | 3351 | // default appertainsTo logic. | 
|  | 3352 | if (Attr.isValueUnset("Subjects")) | 
| Aaron Ballman | 93b5cc6 | 2013-12-02 19:36:42 +0000 | [diff] [blame] | 3353 | return "defaultAppertainsTo"; | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3354 |  | 
|  | 3355 | const Record *SubjectObj = Attr.getValueAsDef("Subjects"); | 
|  | 3356 | std::vector<Record*> Subjects = SubjectObj->getValueAsListOfDefs("Subjects"); | 
|  | 3357 |  | 
|  | 3358 | // If the list of subjects is empty, it is assumed that the attribute | 
|  | 3359 | // appertains to everything. | 
|  | 3360 | if (Subjects.empty()) | 
| Aaron Ballman | 93b5cc6 | 2013-12-02 19:36:42 +0000 | [diff] [blame] | 3361 | return "defaultAppertainsTo"; | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3362 |  | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3363 | bool Warn = SubjectObj->getValueAsDef("Diag")->getValueAsBit("Warn"); | 
|  | 3364 |  | 
|  | 3365 | // Otherwise, generate an appertainsTo check specific to this attribute which | 
|  | 3366 | // checks all of the given subjects against the Decl passed in. Return the | 
|  | 3367 | // name of that check to the caller. | 
| Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 3368 | // | 
|  | 3369 | // If D is null, that means the attribute was not applied to a declaration | 
|  | 3370 | // at all (for instance because it was applied to a type), or that the caller | 
|  | 3371 | // has determined that the check should fail (perhaps prior to the creation | 
|  | 3372 | // of the declaration). | 
| Matthias Braun | bbbf5d4 | 2016-12-04 05:55:09 +0000 | [diff] [blame] | 3373 | std::string FnName = "check" + Attr.getName().str() + "AppertainsTo"; | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3374 | std::stringstream SS; | 
| Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3375 | SS << "static bool " << FnName << "(Sema &S, const ParsedAttr &Attr, "; | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3376 | SS << "const Decl *D) {\n"; | 
| Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 3377 | SS << "  if (!D || ("; | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3378 | for (auto I = Subjects.begin(), E = Subjects.end(); I != E; ++I) { | 
| Aaron Ballman | 8046903 | 2013-11-29 14:57:58 +0000 | [diff] [blame] | 3379 | // If the subject has custom code associated with it, generate a function | 
|  | 3380 | // for it. The function cannot be inlined into this check (yet) because it | 
|  | 3381 | // requires the subject to be of a specific type, and were that information | 
|  | 3382 | // inlined here, it would not support an attribute with multiple custom | 
|  | 3383 | // subjects. | 
|  | 3384 | if ((*I)->isSubClassOf("SubsetSubject")) { | 
|  | 3385 | SS << "!" << GenerateCustomAppertainsTo(**I, OS) << "(D)"; | 
|  | 3386 | } else { | 
| Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 3387 | SS << "!isa<" << GetSubjectWithSuffix(*I) << ">(D)"; | 
| Aaron Ballman | 8046903 | 2013-11-29 14:57:58 +0000 | [diff] [blame] | 3388 | } | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3389 |  | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3390 | if (I + 1 != E) | 
|  | 3391 | SS << " && "; | 
|  | 3392 | } | 
| Richard Smith | f4e248c | 2018-08-01 00:33:25 +0000 | [diff] [blame] | 3393 | SS << ")) {\n"; | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3394 | SS << "    S.Diag(Attr.getLoc(), diag::"; | 
| Aaron Ballman | adf66b6 | 2017-11-26 20:01:12 +0000 | [diff] [blame] | 3395 | SS << (Warn ? "warn_attribute_wrong_decl_type_str" : | 
|  | 3396 | "err_attribute_wrong_decl_type_str"); | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3397 | SS << ")\n"; | 
| Erich Keane | 44bacdf | 2018-08-09 13:21:32 +0000 | [diff] [blame] | 3398 | SS << "      << Attr << "; | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3399 | SS << CalculateDiagnostic(*SubjectObj) << ";\n"; | 
|  | 3400 | SS << "    return false;\n"; | 
|  | 3401 | SS << "  }\n"; | 
|  | 3402 | SS << "  return true;\n"; | 
|  | 3403 | SS << "}\n\n"; | 
|  | 3404 |  | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3405 | OS << SS.str(); | 
|  | 3406 | return FnName; | 
|  | 3407 | } | 
|  | 3408 |  | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3409 | static void | 
|  | 3410 | emitAttributeMatchRules(PragmaClangAttributeSupport &PragmaAttributeSupport, | 
|  | 3411 | raw_ostream &OS) { | 
|  | 3412 | OS << "static bool checkAttributeMatchRuleAppliesTo(const Decl *D, " | 
|  | 3413 | << AttributeSubjectMatchRule::EnumName << " rule) {\n"; | 
|  | 3414 | OS << "  switch (rule) {\n"; | 
|  | 3415 | for (const auto &Rule : PragmaAttributeSupport.Rules) { | 
|  | 3416 | if (Rule.isAbstractRule()) { | 
|  | 3417 | OS << "  case " << Rule.getEnumValue() << ":\n"; | 
|  | 3418 | OS << "    assert(false && \"Abstract matcher rule isn't allowed\");\n"; | 
|  | 3419 | OS << "    return false;\n"; | 
|  | 3420 | continue; | 
|  | 3421 | } | 
|  | 3422 | std::vector<Record *> Subjects = Rule.getSubjects(); | 
|  | 3423 | assert(!Subjects.empty() && "Missing subjects"); | 
|  | 3424 | OS << "  case " << Rule.getEnumValue() << ":\n"; | 
|  | 3425 | OS << "    return "; | 
|  | 3426 | for (auto I = Subjects.begin(), E = Subjects.end(); I != E; ++I) { | 
|  | 3427 | // If the subject has custom code associated with it, use the function | 
|  | 3428 | // that was generated for GenerateAppertainsTo to check if the declaration | 
|  | 3429 | // is valid. | 
|  | 3430 | if ((*I)->isSubClassOf("SubsetSubject")) | 
|  | 3431 | OS << functionNameForCustomAppertainsTo(**I) << "(D)"; | 
|  | 3432 | else | 
|  | 3433 | OS << "isa<" << GetSubjectWithSuffix(*I) << ">(D)"; | 
|  | 3434 |  | 
|  | 3435 | if (I + 1 != E) | 
|  | 3436 | OS << " || "; | 
|  | 3437 | } | 
|  | 3438 | OS << ";\n"; | 
|  | 3439 | } | 
|  | 3440 | OS << "  }\n"; | 
|  | 3441 | OS << "  llvm_unreachable(\"Invalid match rule\");\nreturn false;\n"; | 
|  | 3442 | OS << "}\n\n"; | 
|  | 3443 | } | 
|  | 3444 |  | 
| Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3445 | static void GenerateDefaultLangOptRequirements(raw_ostream &OS) { | 
|  | 3446 | OS << "static bool defaultDiagnoseLangOpts(Sema &, "; | 
| Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3447 | OS << "const ParsedAttr &) {\n"; | 
| Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3448 | OS << "  return true;\n"; | 
|  | 3449 | OS << "}\n\n"; | 
|  | 3450 | } | 
|  | 3451 |  | 
|  | 3452 | static std::string GenerateLangOptRequirements(const Record &R, | 
|  | 3453 | raw_ostream &OS) { | 
|  | 3454 | // If the attribute has an empty or unset list of language requirements, | 
|  | 3455 | // return the default handler. | 
|  | 3456 | std::vector<Record *> LangOpts = R.getValueAsListOfDefs("LangOpts"); | 
|  | 3457 | if (LangOpts.empty()) | 
|  | 3458 | return "defaultDiagnoseLangOpts"; | 
|  | 3459 |  | 
| John McCall | 2c91c3b | 2019-05-30 04:09:01 +0000 | [diff] [blame] | 3460 | // Generate a unique function name for the diagnostic test. The list of | 
|  | 3461 | // options should usually be short (one or two options), and the | 
|  | 3462 | // uniqueness isn't strictly necessary (it is just for codegen efficiency). | 
|  | 3463 | std::string FnName = "check"; | 
|  | 3464 | for (auto I = LangOpts.begin(), E = LangOpts.end(); I != E; ++I) | 
|  | 3465 | FnName += (*I)->getValueAsString("Name"); | 
| Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3466 | FnName += "LangOpts"; | 
|  | 3467 |  | 
|  | 3468 | // If this code has already been generated, simply return the previous | 
|  | 3469 | // instance of it. | 
|  | 3470 | static std::set<std::string> CustomLangOptsSet; | 
| Eugene Zelenko | 5f02b77 | 2015-12-08 18:49:01 +0000 | [diff] [blame] | 3471 | auto I = CustomLangOptsSet.find(FnName); | 
| Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3472 | if (I != CustomLangOptsSet.end()) | 
|  | 3473 | return *I; | 
|  | 3474 |  | 
| Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3475 | OS << "static bool " << FnName << "(Sema &S, const ParsedAttr &Attr) {\n"; | 
| John McCall | 2c91c3b | 2019-05-30 04:09:01 +0000 | [diff] [blame] | 3476 | OS << "  auto &LangOpts = S.LangOpts;\n"; | 
|  | 3477 | OS << "  if (" << GenerateTestExpression(LangOpts) << ")\n"; | 
| Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3478 | OS << "    return true;\n\n"; | 
|  | 3479 | OS << "  S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) "; | 
|  | 3480 | OS << "<< Attr.getName();\n"; | 
|  | 3481 | OS << "  return false;\n"; | 
|  | 3482 | OS << "}\n\n"; | 
|  | 3483 |  | 
|  | 3484 | CustomLangOptsSet.insert(FnName); | 
|  | 3485 | return FnName; | 
|  | 3486 | } | 
|  | 3487 |  | 
| Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3488 | static void GenerateDefaultTargetRequirements(raw_ostream &OS) { | 
| Bob Wilson | 7c73083 | 2015-07-20 22:57:31 +0000 | [diff] [blame] | 3489 | OS << "static bool defaultTargetRequirements(const TargetInfo &) {\n"; | 
| Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3490 | OS << "  return true;\n"; | 
|  | 3491 | OS << "}\n\n"; | 
|  | 3492 | } | 
|  | 3493 |  | 
|  | 3494 | static std::string GenerateTargetRequirements(const Record &Attr, | 
|  | 3495 | const ParsedAttrMap &Dupes, | 
|  | 3496 | raw_ostream &OS) { | 
|  | 3497 | // If the attribute is not a target specific attribute, return the default | 
|  | 3498 | // target handler. | 
|  | 3499 | if (!Attr.isSubClassOf("TargetSpecificAttr")) | 
|  | 3500 | return "defaultTargetRequirements"; | 
|  | 3501 |  | 
|  | 3502 | // Get the list of architectures to be tested for. | 
|  | 3503 | const Record *R = Attr.getValueAsDef("Target"); | 
| Craig Topper | 0064858 | 2017-05-31 19:01:22 +0000 | [diff] [blame] | 3504 | std::vector<StringRef> Arches = R->getValueAsListOfStrings("Arches"); | 
| Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3505 |  | 
|  | 3506 | // If there are other attributes which share the same parsed attribute kind, | 
|  | 3507 | // such as target-specific attributes with a shared spelling, collapse the | 
|  | 3508 | // duplicate architectures. This is required because a shared target-specific | 
| Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3509 | // attribute has only one ParsedAttr::Kind enumeration value, but it | 
| Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3510 | // applies to multiple target architectures. In order for the attribute to be | 
|  | 3511 | // considered valid, all of its architectures need to be included. | 
|  | 3512 | if (!Attr.isValueUnset("ParseKind")) { | 
| Erich Keane | 3bff414 | 2017-10-16 23:25:24 +0000 | [diff] [blame] | 3513 | const StringRef APK = Attr.getValueAsString("ParseKind"); | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3514 | for (const auto &I : Dupes) { | 
|  | 3515 | if (I.first == APK) { | 
| Craig Topper | 0064858 | 2017-05-31 19:01:22 +0000 | [diff] [blame] | 3516 | std::vector<StringRef> DA = | 
|  | 3517 | I.second->getValueAsDef("Target")->getValueAsListOfStrings( | 
|  | 3518 | "Arches"); | 
|  | 3519 | Arches.insert(Arches.end(), DA.begin(), DA.end()); | 
| Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3520 | } | 
|  | 3521 | } | 
|  | 3522 | } | 
|  | 3523 |  | 
| Bob Wilson | 0058b82 | 2015-07-20 22:57:36 +0000 | [diff] [blame] | 3524 | std::string FnName = "isTarget"; | 
|  | 3525 | std::string Test; | 
| Richard Smith | 78b239e | 2019-06-20 20:44:45 +0000 | [diff] [blame] | 3526 | bool UsesT = GenerateTargetSpecificAttrChecks(R, Arches, Test, &FnName); | 
| Bob Wilson | 7c73083 | 2015-07-20 22:57:31 +0000 | [diff] [blame] | 3527 |  | 
| Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3528 | // If this code has already been generated, simply return the previous | 
|  | 3529 | // instance of it. | 
|  | 3530 | static std::set<std::string> CustomTargetSet; | 
| Eugene Zelenko | 5f02b77 | 2015-12-08 18:49:01 +0000 | [diff] [blame] | 3531 | auto I = CustomTargetSet.find(FnName); | 
| Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3532 | if (I != CustomTargetSet.end()) | 
|  | 3533 | return *I; | 
|  | 3534 |  | 
| Bob Wilson | 7c73083 | 2015-07-20 22:57:31 +0000 | [diff] [blame] | 3535 | OS << "static bool " << FnName << "(const TargetInfo &Target) {\n"; | 
| Richard Smith | 78b239e | 2019-06-20 20:44:45 +0000 | [diff] [blame] | 3536 | if (UsesT) | 
|  | 3537 | OS << "  const llvm::Triple &T = Target.getTriple(); (void)T;\n"; | 
| Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3538 | OS << "  return " << Test << ";\n"; | 
|  | 3539 | OS << "}\n\n"; | 
|  | 3540 |  | 
|  | 3541 | CustomTargetSet.insert(FnName); | 
|  | 3542 | return FnName; | 
|  | 3543 | } | 
|  | 3544 |  | 
| Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 3545 | static void GenerateDefaultSpellingIndexToSemanticSpelling(raw_ostream &OS) { | 
|  | 3546 | OS << "static unsigned defaultSpellingIndexToSemanticSpelling(" | 
| Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3547 | << "const ParsedAttr &Attr) {\n"; | 
| Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 3548 | OS << "  return UINT_MAX;\n"; | 
|  | 3549 | OS << "}\n\n"; | 
|  | 3550 | } | 
|  | 3551 |  | 
|  | 3552 | static std::string GenerateSpellingIndexToSemanticSpelling(const Record &Attr, | 
|  | 3553 | raw_ostream &OS) { | 
|  | 3554 | // If the attribute does not have a semantic form, we can bail out early. | 
|  | 3555 | if (!Attr.getValueAsBit("ASTNode")) | 
|  | 3556 | return "defaultSpellingIndexToSemanticSpelling"; | 
|  | 3557 |  | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 3558 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr); | 
| Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 3559 |  | 
|  | 3560 | // If there are zero or one spellings, or all of the spellings share the same | 
|  | 3561 | // name, we can also bail out early. | 
|  | 3562 | if (Spellings.size() <= 1 || SpellingNamesAreCommon(Spellings)) | 
|  | 3563 | return "defaultSpellingIndexToSemanticSpelling"; | 
|  | 3564 |  | 
|  | 3565 | // Generate the enumeration we will use for the mapping. | 
|  | 3566 | SemanticSpellingMap SemanticToSyntacticMap; | 
|  | 3567 | std::string Enum = CreateSemanticSpellings(Spellings, SemanticToSyntacticMap); | 
| Matthias Braun | bbbf5d4 | 2016-12-04 05:55:09 +0000 | [diff] [blame] | 3568 | std::string Name = Attr.getName().str() + "AttrSpellingMap"; | 
| Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 3569 |  | 
| Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3570 | OS << "static unsigned " << Name << "(const ParsedAttr &Attr) {\n"; | 
| Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 3571 | OS << Enum; | 
|  | 3572 | OS << "  unsigned Idx = Attr.getAttributeSpellingListIndex();\n"; | 
|  | 3573 | WriteSemanticSpellingSwitch("Idx", SemanticToSyntacticMap, OS); | 
|  | 3574 | OS << "}\n\n"; | 
|  | 3575 |  | 
|  | 3576 | return Name; | 
|  | 3577 | } | 
|  | 3578 |  | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 3579 | static bool IsKnownToGCC(const Record &Attr) { | 
|  | 3580 | // Look at the spellings for this subject; if there are any spellings which | 
|  | 3581 | // claim to be known to GCC, the attribute is known to GCC. | 
| George Burgess IV | 1881a57 | 2016-12-01 00:13:18 +0000 | [diff] [blame] | 3582 | return llvm::any_of( | 
|  | 3583 | GetFlattenedSpellings(Attr), | 
|  | 3584 | [](const FlattenedSpelling &S) { return S.knownToGCC(); }); | 
| Aaron Ballman | 9a99e0d | 2014-01-20 17:18:35 +0000 | [diff] [blame] | 3585 | } | 
|  | 3586 |  | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3587 | /// Emits the parsed attribute helpers | 
|  | 3588 | void EmitClangAttrParsedAttrImpl(RecordKeeper &Records, raw_ostream &OS) { | 
|  | 3589 | emitSourceFileHeader("Parsed attribute helpers", OS); | 
|  | 3590 |  | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3591 | PragmaClangAttributeSupport &PragmaAttributeSupport = | 
|  | 3592 | getPragmaAttributeSupport(Records); | 
|  | 3593 |  | 
| Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3594 | // Get the list of parsed attributes, and accept the optional list of | 
|  | 3595 | // duplicates due to the ParseKind. | 
|  | 3596 | ParsedAttrMap Dupes; | 
|  | 3597 | ParsedAttrMap Attrs = getParsedAttrList(Records, &Dupes); | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3598 |  | 
| Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 3599 | // Generate the default appertainsTo, target and language option diagnostic, | 
|  | 3600 | // and spelling list index mapping methods. | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3601 | GenerateDefaultAppertainsTo(OS); | 
| Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3602 | GenerateDefaultLangOptRequirements(OS); | 
| Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3603 | GenerateDefaultTargetRequirements(OS); | 
| Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 3604 | GenerateDefaultSpellingIndexToSemanticSpelling(OS); | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3605 |  | 
|  | 3606 | // Generate the appertainsTo diagnostic methods and write their names into | 
|  | 3607 | // another mapping. At the same time, generate the AttrInfoMap object | 
|  | 3608 | // contents. Due to the reliance on generated code, use separate streams so | 
|  | 3609 | // that code will not be interleaved. | 
| Erich Keane | df9e8ae | 2017-10-16 22:47:26 +0000 | [diff] [blame] | 3610 | std::string Buffer; | 
|  | 3611 | raw_string_ostream SS {Buffer}; | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3612 | for (auto I = Attrs.begin(), E = Attrs.end(); I != E; ++I) { | 
| Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3613 | // TODO: If the attribute's kind appears in the list of duplicates, that is | 
|  | 3614 | // because it is a target-specific attribute that appears multiple times. | 
|  | 3615 | // It would be beneficial to test whether the duplicates are "similar | 
|  | 3616 | // enough" to each other to not cause problems. For instance, check that | 
| Alp Toker | 96cf758 | 2014-01-18 21:49:37 +0000 | [diff] [blame] | 3617 | // the spellings are identical, and custom parsing rules match, etc. | 
| Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3618 |  | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3619 | // We need to generate struct instances based off ParsedAttrInfo from | 
| Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3620 | // ParsedAttr.cpp. | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3621 | SS << "  { "; | 
|  | 3622 | emitArgInfo(*I->second, SS); | 
|  | 3623 | SS << ", " << I->second->getValueAsBit("HasCustomParsing"); | 
| Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3624 | SS << ", " << I->second->isSubClassOf("TargetSpecificAttr"); | 
| Aaron Ballman | b9a457a | 2018-05-03 15:33:50 +0000 | [diff] [blame] | 3625 | SS << ", " | 
|  | 3626 | << (I->second->isSubClassOf("TypeAttr") || | 
|  | 3627 | I->second->isSubClassOf("DeclOrTypeAttr")); | 
| Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 3628 | SS << ", " << I->second->isSubClassOf("StmtAttr"); | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 3629 | SS << ", " << IsKnownToGCC(*I->second); | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3630 | SS << ", " << PragmaAttributeSupport.isAttributedSupported(*I->second); | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3631 | SS << ", " << GenerateAppertainsTo(*I->second, OS); | 
| Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3632 | SS << ", " << GenerateLangOptRequirements(*I->second, OS); | 
| Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3633 | SS << ", " << GenerateTargetRequirements(*I->second, Dupes, OS); | 
| Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 3634 | SS << ", " << GenerateSpellingIndexToSemanticSpelling(*I->second, OS); | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3635 | SS << ", " | 
|  | 3636 | << PragmaAttributeSupport.generateStrictConformsTo(*I->second, OS); | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3637 | SS << " }"; | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3638 |  | 
|  | 3639 | if (I + 1 != E) | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3640 | SS << ","; | 
|  | 3641 |  | 
|  | 3642 | SS << "  // AT_" << I->first << "\n"; | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3643 | } | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3644 |  | 
| Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3645 | OS << "static const ParsedAttrInfo AttrInfoMap[ParsedAttr::UnknownAttribute " | 
|  | 3646 | "+ 1] = {\n"; | 
| Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3647 | OS << SS.str(); | 
| Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3648 | OS << "};\n\n"; | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3649 |  | 
|  | 3650 | // Generate the attribute match rules. | 
|  | 3651 | emitAttributeMatchRules(PragmaAttributeSupport, OS); | 
| Michael Han | 4a04517 | 2012-03-07 00:12:16 +0000 | [diff] [blame] | 3652 | } | 
|  | 3653 |  | 
| Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 3654 | // Emits the kind list of parsed attributes | 
|  | 3655 | void EmitClangAttrParsedAttrKinds(RecordKeeper &Records, raw_ostream &OS) { | 
| Dmitri Gribenko | 6b11fca | 2013-01-30 21:54:20 +0000 | [diff] [blame] | 3656 | emitSourceFileHeader("Attribute name matcher", OS); | 
|  | 3657 |  | 
| Aaron Ballman | 09e98ff | 2014-01-13 21:42:39 +0000 | [diff] [blame] | 3658 | std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); | 
| Nico Weber | 20e0804 | 2016-09-03 02:55:10 +0000 | [diff] [blame] | 3659 | std::vector<StringMatcher::StringPair> GNU, Declspec, Microsoft, CXX11, | 
| Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 3660 | Keywords, Pragma, C2x; | 
| Aaron Ballman | 64e6986 | 2013-12-15 13:05:48 +0000 | [diff] [blame] | 3661 | std::set<std::string> Seen; | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 3662 | for (const auto *A : Attrs) { | 
|  | 3663 | const Record &Attr = *A; | 
| Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 3664 |  | 
| Michael Han | 4a04517 | 2012-03-07 00:12:16 +0000 | [diff] [blame] | 3665 | bool SemaHandler = Attr.getValueAsBit("SemaHandler"); | 
| Douglas Gregor | 19fbb8f | 2012-05-02 16:18:45 +0000 | [diff] [blame] | 3666 | bool Ignored = Attr.getValueAsBit("Ignored"); | 
| Douglas Gregor | 19fbb8f | 2012-05-02 16:18:45 +0000 | [diff] [blame] | 3667 | if (SemaHandler || Ignored) { | 
| Aaron Ballman | 09e98ff | 2014-01-13 21:42:39 +0000 | [diff] [blame] | 3668 | // Attribute spellings can be shared between target-specific attributes, | 
|  | 3669 | // and can be shared between syntaxes for the same attribute. For | 
|  | 3670 | // instance, an attribute can be spelled GNU<"interrupt"> for an ARM- | 
|  | 3671 | // specific attribute, or MSP430-specific attribute. Additionally, an | 
|  | 3672 | // attribute can be spelled GNU<"dllexport"> and Declspec<"dllexport"> | 
|  | 3673 | // for the same semantic attribute. Ultimately, we need to map each of | 
| Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3674 | // these to a single ParsedAttr::Kind value, but the StringMatcher | 
| Aaron Ballman | 09e98ff | 2014-01-13 21:42:39 +0000 | [diff] [blame] | 3675 | // class cannot handle duplicate match strings. So we generate a list of | 
|  | 3676 | // string to match based on the syntax, and emit multiple string matchers | 
|  | 3677 | // depending on the syntax used. | 
| Aaron Ballman | 64e6986 | 2013-12-15 13:05:48 +0000 | [diff] [blame] | 3678 | std::string AttrName; | 
|  | 3679 | if (Attr.isSubClassOf("TargetSpecificAttr") && | 
|  | 3680 | !Attr.isValueUnset("ParseKind")) { | 
|  | 3681 | AttrName = Attr.getValueAsString("ParseKind"); | 
|  | 3682 | if (Seen.find(AttrName) != Seen.end()) | 
|  | 3683 | continue; | 
|  | 3684 | Seen.insert(AttrName); | 
|  | 3685 | } else | 
|  | 3686 | AttrName = NormalizeAttrName(StringRef(Attr.getName())).str(); | 
|  | 3687 |  | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 3688 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr); | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3689 | for (const auto &S : Spellings) { | 
| Benjamin Kramer | 2e018ef | 2016-05-27 13:36:58 +0000 | [diff] [blame] | 3690 | const std::string &RawSpelling = S.name(); | 
| Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 3691 | std::vector<StringMatcher::StringPair> *Matches = nullptr; | 
| Benjamin Kramer | 2e018ef | 2016-05-27 13:36:58 +0000 | [diff] [blame] | 3692 | std::string Spelling; | 
|  | 3693 | const std::string &Variety = S.variety(); | 
| Aaron Ballman | 09e98ff | 2014-01-13 21:42:39 +0000 | [diff] [blame] | 3694 | if (Variety == "CXX11") { | 
|  | 3695 | Matches = &CXX11; | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3696 | Spelling += S.nameSpace(); | 
| Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3697 | Spelling += "::"; | 
| Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 3698 | } else if (Variety == "C2x") { | 
|  | 3699 | Matches = &C2x; | 
|  | 3700 | Spelling += S.nameSpace(); | 
|  | 3701 | Spelling += "::"; | 
| Aaron Ballman | 09e98ff | 2014-01-13 21:42:39 +0000 | [diff] [blame] | 3702 | } else if (Variety == "GNU") | 
|  | 3703 | Matches = &GNU; | 
|  | 3704 | else if (Variety == "Declspec") | 
|  | 3705 | Matches = &Declspec; | 
| Nico Weber | 20e0804 | 2016-09-03 02:55:10 +0000 | [diff] [blame] | 3706 | else if (Variety == "Microsoft") | 
|  | 3707 | Matches = &Microsoft; | 
| Aaron Ballman | 09e98ff | 2014-01-13 21:42:39 +0000 | [diff] [blame] | 3708 | else if (Variety == "Keyword") | 
|  | 3709 | Matches = &Keywords; | 
| Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 3710 | else if (Variety == "Pragma") | 
|  | 3711 | Matches = &Pragma; | 
| Alexis Hunt | a0e54d4 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 3712 |  | 
| Aaron Ballman | 09e98ff | 2014-01-13 21:42:39 +0000 | [diff] [blame] | 3713 | assert(Matches && "Unsupported spelling variety found"); | 
|  | 3714 |  | 
| Justin Lebar | 4086fe5 | 2017-01-05 16:51:54 +0000 | [diff] [blame] | 3715 | if (Variety == "GNU") | 
|  | 3716 | Spelling += NormalizeGNUAttrSpelling(RawSpelling); | 
|  | 3717 | else | 
|  | 3718 | Spelling += RawSpelling; | 
|  | 3719 |  | 
| Douglas Gregor | 19fbb8f | 2012-05-02 16:18:45 +0000 | [diff] [blame] | 3720 | if (SemaHandler) | 
| Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3721 | Matches->push_back(StringMatcher::StringPair( | 
|  | 3722 | Spelling, "return ParsedAttr::AT_" + AttrName + ";")); | 
| Douglas Gregor | 19fbb8f | 2012-05-02 16:18:45 +0000 | [diff] [blame] | 3723 | else | 
| Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3724 | Matches->push_back(StringMatcher::StringPair( | 
|  | 3725 | Spelling, "return ParsedAttr::IgnoredAttribute;")); | 
| Michael Han | 4a04517 | 2012-03-07 00:12:16 +0000 | [diff] [blame] | 3726 | } | 
|  | 3727 | } | 
|  | 3728 | } | 
| Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3729 |  | 
|  | 3730 | OS << "static ParsedAttr::Kind getAttrKind(StringRef Name, "; | 
|  | 3731 | OS << "ParsedAttr::Syntax Syntax) {\n"; | 
|  | 3732 | OS << "  if (ParsedAttr::AS_GNU == Syntax) {\n"; | 
| Aaron Ballman | 09e98ff | 2014-01-13 21:42:39 +0000 | [diff] [blame] | 3733 | StringMatcher("Name", GNU, OS).Emit(); | 
| Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3734 | OS << "  } else if (ParsedAttr::AS_Declspec == Syntax) {\n"; | 
| Aaron Ballman | 09e98ff | 2014-01-13 21:42:39 +0000 | [diff] [blame] | 3735 | StringMatcher("Name", Declspec, OS).Emit(); | 
| Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3736 | OS << "  } else if (ParsedAttr::AS_Microsoft == Syntax) {\n"; | 
| Nico Weber | 20e0804 | 2016-09-03 02:55:10 +0000 | [diff] [blame] | 3737 | StringMatcher("Name", Microsoft, OS).Emit(); | 
| Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3738 | OS << "  } else if (ParsedAttr::AS_CXX11 == Syntax) {\n"; | 
| Aaron Ballman | 09e98ff | 2014-01-13 21:42:39 +0000 | [diff] [blame] | 3739 | StringMatcher("Name", CXX11, OS).Emit(); | 
| Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3740 | OS << "  } else if (ParsedAttr::AS_C2x == Syntax) {\n"; | 
| Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 3741 | StringMatcher("Name", C2x, OS).Emit(); | 
| Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3742 | OS << "  } else if (ParsedAttr::AS_Keyword == Syntax || "; | 
|  | 3743 | OS << "ParsedAttr::AS_ContextSensitiveKeyword == Syntax) {\n"; | 
| Aaron Ballman | 09e98ff | 2014-01-13 21:42:39 +0000 | [diff] [blame] | 3744 | StringMatcher("Name", Keywords, OS).Emit(); | 
| Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3745 | OS << "  } else if (ParsedAttr::AS_Pragma == Syntax) {\n"; | 
| Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 3746 | StringMatcher("Name", Pragma, OS).Emit(); | 
| Aaron Ballman | 09e98ff | 2014-01-13 21:42:39 +0000 | [diff] [blame] | 3747 | OS << "  }\n"; | 
| Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3748 | OS << "  return ParsedAttr::UnknownAttribute;\n" | 
| Douglas Gregor | 377f99b | 2012-05-02 17:33:51 +0000 | [diff] [blame] | 3749 | << "}\n"; | 
| Michael Han | 4a04517 | 2012-03-07 00:12:16 +0000 | [diff] [blame] | 3750 | } | 
|  | 3751 |  | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 3752 | // Emits the code to dump an attribute. | 
| Stephen Kelly | db8fac1 | 2019-01-11 19:16:01 +0000 | [diff] [blame] | 3753 | void EmitClangAttrTextNodeDump(RecordKeeper &Records, raw_ostream &OS) { | 
|  | 3754 | emitSourceFileHeader("Attribute text node dumper", OS); | 
| Dmitri Gribenko | 6b11fca | 2013-01-30 21:54:20 +0000 | [diff] [blame] | 3755 |  | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 3756 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args; | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 3757 | for (const auto *Attr : Attrs) { | 
|  | 3758 | const Record &R = *Attr; | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 3759 | if (!R.getValueAsBit("ASTNode")) | 
|  | 3760 | continue; | 
| Aaron Ballman | bc90961 | 2014-01-22 21:51:20 +0000 | [diff] [blame] | 3761 |  | 
|  | 3762 | // If the attribute has a semantically-meaningful name (which is determined | 
|  | 3763 | // by whether there is a Spelling enumeration for it), then write out the | 
|  | 3764 | // spelling used for the attribute. | 
| Stephen Kelly | db8fac1 | 2019-01-11 19:16:01 +0000 | [diff] [blame] | 3765 |  | 
|  | 3766 | std::string FunctionContent; | 
|  | 3767 | llvm::raw_string_ostream SS(FunctionContent); | 
|  | 3768 |  | 
| Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 3769 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R); | 
| Aaron Ballman | bc90961 | 2014-01-22 21:51:20 +0000 | [diff] [blame] | 3770 | if (Spellings.size() > 1 && !SpellingNamesAreCommon(Spellings)) | 
| Stephen Kelly | db8fac1 | 2019-01-11 19:16:01 +0000 | [diff] [blame] | 3771 | SS << "    OS << \" \" << A->getSpelling();\n"; | 
| Aaron Ballman | bc90961 | 2014-01-22 21:51:20 +0000 | [diff] [blame] | 3772 |  | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 3773 | Args = R.getValueAsListOfDefs("Args"); | 
| Stephen Kelly | db8fac1 | 2019-01-11 19:16:01 +0000 | [diff] [blame] | 3774 | for (const auto *Arg : Args) | 
|  | 3775 | createArgument(*Arg, R.getName())->writeDump(SS); | 
| Richard Trieu | de5cc7d | 2013-01-31 01:44:26 +0000 | [diff] [blame] | 3776 |  | 
| Stephen Kelly | db8fac1 | 2019-01-11 19:16:01 +0000 | [diff] [blame] | 3777 | if (SS.tell()) { | 
|  | 3778 | OS << "  void Visit" << R.getName() << "Attr(const " << R.getName() | 
|  | 3779 | << "Attr *A) {\n"; | 
|  | 3780 | if (!Args.empty()) | 
|  | 3781 | OS << "    const auto *SA = cast<" << R.getName() | 
|  | 3782 | << "Attr>(A); (void)SA;\n"; | 
|  | 3783 | OS << SS.str(); | 
|  | 3784 | OS << "  }\n"; | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 3785 | } | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 3786 | } | 
| Stephen Kelly | db8fac1 | 2019-01-11 19:16:01 +0000 | [diff] [blame] | 3787 | } | 
|  | 3788 |  | 
|  | 3789 | void EmitClangAttrNodeTraverse(RecordKeeper &Records, raw_ostream &OS) { | 
|  | 3790 | emitSourceFileHeader("Attribute text node traverser", OS); | 
|  | 3791 |  | 
|  | 3792 | std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"), Args; | 
|  | 3793 | for (const auto *Attr : Attrs) { | 
|  | 3794 | const Record &R = *Attr; | 
|  | 3795 | if (!R.getValueAsBit("ASTNode")) | 
|  | 3796 | continue; | 
|  | 3797 |  | 
|  | 3798 | std::string FunctionContent; | 
|  | 3799 | llvm::raw_string_ostream SS(FunctionContent); | 
|  | 3800 |  | 
|  | 3801 | Args = R.getValueAsListOfDefs("Args"); | 
|  | 3802 | for (const auto *Arg : Args) | 
|  | 3803 | createArgument(*Arg, R.getName())->writeDumpChildren(SS); | 
|  | 3804 | if (SS.tell()) { | 
|  | 3805 | OS << "  void Visit" << R.getName() << "Attr(const " << R.getName() | 
|  | 3806 | << "Attr *A) {\n"; | 
|  | 3807 | if (!Args.empty()) | 
|  | 3808 | OS << "    const auto *SA = cast<" << R.getName() | 
|  | 3809 | << "Attr>(A); (void)SA;\n"; | 
|  | 3810 | OS << SS.str(); | 
|  | 3811 | OS << "  }\n"; | 
|  | 3812 | } | 
|  | 3813 | } | 
| Alexander Kornienko | 5bc364e | 2013-01-07 17:53:08 +0000 | [diff] [blame] | 3814 | } | 
|  | 3815 |  | 
| Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 3816 | void EmitClangAttrParserStringSwitches(RecordKeeper &Records, | 
|  | 3817 | raw_ostream &OS) { | 
|  | 3818 | emitSourceFileHeader("Parser-related llvm::StringSwitch cases", OS); | 
|  | 3819 | emitClangAttrArgContextList(Records, OS); | 
|  | 3820 | emitClangAttrIdentifierArgList(Records, OS); | 
| Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 3821 | emitClangAttrVariadicIdentifierArgList(Records, OS); | 
| Johannes Doerfert | ac991bb | 2019-01-19 05:36:54 +0000 | [diff] [blame] | 3822 | emitClangAttrThisIsaIdentifierArgList(Records, OS); | 
| Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 3823 | emitClangAttrTypeArgList(Records, OS); | 
|  | 3824 | emitClangAttrLateParsedList(Records, OS); | 
|  | 3825 | } | 
|  | 3826 |  | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 3827 | void EmitClangAttrSubjectMatchRulesParserStringSwitches(RecordKeeper &Records, | 
|  | 3828 | raw_ostream &OS) { | 
|  | 3829 | getPragmaAttributeSupport(Records).generateParsingHelpers(OS); | 
|  | 3830 | } | 
|  | 3831 |  | 
| Richard Smith | 09ac4a1 | 2018-08-30 19:16:33 +0000 | [diff] [blame] | 3832 | enum class SpellingKind { | 
|  | 3833 | GNU, | 
|  | 3834 | CXX11, | 
|  | 3835 | C2x, | 
|  | 3836 | Declspec, | 
|  | 3837 | Microsoft, | 
|  | 3838 | Keyword, | 
|  | 3839 | Pragma, | 
|  | 3840 | }; | 
|  | 3841 | static const size_t NumSpellingKinds = (size_t)SpellingKind::Pragma + 1; | 
|  | 3842 |  | 
|  | 3843 | class SpellingList { | 
|  | 3844 | std::vector<std::string> Spellings[NumSpellingKinds]; | 
|  | 3845 |  | 
|  | 3846 | public: | 
|  | 3847 | ArrayRef<std::string> operator[](SpellingKind K) const { | 
|  | 3848 | return Spellings[(size_t)K]; | 
|  | 3849 | } | 
|  | 3850 |  | 
|  | 3851 | void add(const Record &Attr, FlattenedSpelling Spelling) { | 
|  | 3852 | SpellingKind Kind = StringSwitch<SpellingKind>(Spelling.variety()) | 
|  | 3853 | .Case("GNU", SpellingKind::GNU) | 
|  | 3854 | .Case("CXX11", SpellingKind::CXX11) | 
|  | 3855 | .Case("C2x", SpellingKind::C2x) | 
|  | 3856 | .Case("Declspec", SpellingKind::Declspec) | 
|  | 3857 | .Case("Microsoft", SpellingKind::Microsoft) | 
|  | 3858 | .Case("Keyword", SpellingKind::Keyword) | 
|  | 3859 | .Case("Pragma", SpellingKind::Pragma); | 
|  | 3860 | std::string Name; | 
|  | 3861 | if (!Spelling.nameSpace().empty()) { | 
|  | 3862 | switch (Kind) { | 
|  | 3863 | case SpellingKind::CXX11: | 
|  | 3864 | case SpellingKind::C2x: | 
|  | 3865 | Name = Spelling.nameSpace() + "::"; | 
|  | 3866 | break; | 
|  | 3867 | case SpellingKind::Pragma: | 
|  | 3868 | Name = Spelling.nameSpace() + " "; | 
|  | 3869 | break; | 
|  | 3870 | default: | 
|  | 3871 | PrintFatalError(Attr.getLoc(), "Unexpected namespace in spelling"); | 
|  | 3872 | } | 
|  | 3873 | } | 
|  | 3874 | Name += Spelling.name(); | 
|  | 3875 |  | 
|  | 3876 | Spellings[(size_t)Kind].push_back(Name); | 
|  | 3877 | } | 
|  | 3878 | }; | 
|  | 3879 |  | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3880 | class DocumentationData { | 
|  | 3881 | public: | 
| Aaron Ballman | 1a3e585 | 2014-02-17 16:18:32 +0000 | [diff] [blame] | 3882 | const Record *Documentation; | 
|  | 3883 | const Record *Attribute; | 
| Erich Keane | a98a2be | 2017-10-16 20:31:05 +0000 | [diff] [blame] | 3884 | std::string Heading; | 
| Richard Smith | 09ac4a1 | 2018-08-30 19:16:33 +0000 | [diff] [blame] | 3885 | SpellingList SupportedSpellings; | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3886 |  | 
| Erich Keane | a98a2be | 2017-10-16 20:31:05 +0000 | [diff] [blame] | 3887 | DocumentationData(const Record &Documentation, const Record &Attribute, | 
| Richard Smith | 09ac4a1 | 2018-08-30 19:16:33 +0000 | [diff] [blame] | 3888 | std::pair<std::string, SpellingList> HeadingAndSpellings) | 
| Erich Keane | a98a2be | 2017-10-16 20:31:05 +0000 | [diff] [blame] | 3889 | : Documentation(&Documentation), Attribute(&Attribute), | 
| Richard Smith | 09ac4a1 | 2018-08-30 19:16:33 +0000 | [diff] [blame] | 3890 | Heading(std::move(HeadingAndSpellings.first)), | 
|  | 3891 | SupportedSpellings(std::move(HeadingAndSpellings.second)) {} | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3892 | }; | 
|  | 3893 |  | 
| Aaron Ballman | 4de1b58 | 2014-02-19 22:59:32 +0000 | [diff] [blame] | 3894 | static void WriteCategoryHeader(const Record *DocCategory, | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3895 | raw_ostream &OS) { | 
| Erich Keane | 3bff414 | 2017-10-16 23:25:24 +0000 | [diff] [blame] | 3896 | const StringRef Name = DocCategory->getValueAsString("Name"); | 
|  | 3897 | OS << Name << "\n" << std::string(Name.size(), '=') << "\n"; | 
| Aaron Ballman | 4de1b58 | 2014-02-19 22:59:32 +0000 | [diff] [blame] | 3898 |  | 
|  | 3899 | // If there is content, print that as well. | 
| Erich Keane | 3bff414 | 2017-10-16 23:25:24 +0000 | [diff] [blame] | 3900 | const StringRef ContentStr = DocCategory->getValueAsString("Content"); | 
| Benjamin Kramer | 5c40407 | 2015-04-10 21:37:21 +0000 | [diff] [blame] | 3901 | // Trim leading and trailing newlines and spaces. | 
| Erich Keane | 3bff414 | 2017-10-16 23:25:24 +0000 | [diff] [blame] | 3902 | OS << ContentStr.trim(); | 
| Benjamin Kramer | 5c40407 | 2015-04-10 21:37:21 +0000 | [diff] [blame] | 3903 |  | 
| Aaron Ballman | 4de1b58 | 2014-02-19 22:59:32 +0000 | [diff] [blame] | 3904 | OS << "\n\n"; | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3905 | } | 
|  | 3906 |  | 
| Richard Smith | 09ac4a1 | 2018-08-30 19:16:33 +0000 | [diff] [blame] | 3907 | static std::pair<std::string, SpellingList> | 
|  | 3908 | GetAttributeHeadingAndSpellings(const Record &Documentation, | 
|  | 3909 | const Record &Attribute) { | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3910 | // FIXME: there is no way to have a per-spelling category for the attribute | 
|  | 3911 | // documentation. This may not be a limiting factor since the spellings | 
|  | 3912 | // should generally be consistently applied across the category. | 
|  | 3913 |  | 
| Erich Keane | a98a2be | 2017-10-16 20:31:05 +0000 | [diff] [blame] | 3914 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attribute); | 
| Richard Smith | 09ac4a1 | 2018-08-30 19:16:33 +0000 | [diff] [blame] | 3915 | if (Spellings.empty()) | 
|  | 3916 | PrintFatalError(Attribute.getLoc(), | 
|  | 3917 | "Attribute has no supported spellings; cannot be " | 
|  | 3918 | "documented"); | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3919 |  | 
|  | 3920 | // Determine the heading to be used for this attribute. | 
| Erich Keane | a98a2be | 2017-10-16 20:31:05 +0000 | [diff] [blame] | 3921 | std::string Heading = Documentation.getValueAsString("Heading"); | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3922 | if (Heading.empty()) { | 
|  | 3923 | // If there's only one spelling, we can simply use that. | 
|  | 3924 | if (Spellings.size() == 1) | 
|  | 3925 | Heading = Spellings.begin()->name(); | 
|  | 3926 | else { | 
|  | 3927 | std::set<std::string> Uniques; | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 3928 | for (auto I = Spellings.begin(), E = Spellings.end(); | 
|  | 3929 | I != E && Uniques.size() <= 1; ++I) { | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3930 | std::string Spelling = NormalizeNameForSpellingComparison(I->name()); | 
|  | 3931 | Uniques.insert(Spelling); | 
|  | 3932 | } | 
|  | 3933 | // If the semantic map has only one spelling, that is sufficient for our | 
|  | 3934 | // needs. | 
|  | 3935 | if (Uniques.size() == 1) | 
|  | 3936 | Heading = *Uniques.begin(); | 
|  | 3937 | } | 
|  | 3938 | } | 
|  | 3939 |  | 
|  | 3940 | // If the heading is still empty, it is an error. | 
|  | 3941 | if (Heading.empty()) | 
| Erich Keane | a98a2be | 2017-10-16 20:31:05 +0000 | [diff] [blame] | 3942 | PrintFatalError(Attribute.getLoc(), | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3943 | "This attribute requires a heading to be specified"); | 
|  | 3944 |  | 
| Richard Smith | 09ac4a1 | 2018-08-30 19:16:33 +0000 | [diff] [blame] | 3945 | SpellingList SupportedSpellings; | 
|  | 3946 | for (const auto &I : Spellings) | 
|  | 3947 | SupportedSpellings.add(Attribute, I); | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3948 |  | 
| Richard Smith | 09ac4a1 | 2018-08-30 19:16:33 +0000 | [diff] [blame] | 3949 | return std::make_pair(std::move(Heading), std::move(SupportedSpellings)); | 
| Erich Keane | a98a2be | 2017-10-16 20:31:05 +0000 | [diff] [blame] | 3950 | } | 
|  | 3951 |  | 
|  | 3952 | static void WriteDocumentation(RecordKeeper &Records, | 
|  | 3953 | const DocumentationData &Doc, raw_ostream &OS) { | 
|  | 3954 | OS << Doc.Heading << "\n" << std::string(Doc.Heading.length(), '-') << "\n"; | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3955 |  | 
|  | 3956 | // List what spelling syntaxes the attribute supports. | 
|  | 3957 | OS << ".. csv-table:: Supported Syntaxes\n"; | 
| Richard Smith | 09ac4a1 | 2018-08-30 19:16:33 +0000 | [diff] [blame] | 3958 | OS << "   :header: \"GNU\", \"C++11\", \"C2x\", \"``__declspec``\","; | 
|  | 3959 | OS << " \"Keyword\", \"``#pragma``\", \"``#pragma clang attribute``\"\n\n"; | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3960 | OS << "   \""; | 
| Richard Smith | 09ac4a1 | 2018-08-30 19:16:33 +0000 | [diff] [blame] | 3961 | for (size_t Kind = 0; Kind != NumSpellingKinds; ++Kind) { | 
|  | 3962 | SpellingKind K = (SpellingKind)Kind; | 
| Richard Smith | 23ff7e8 | 2018-08-30 19:19:15 +0000 | [diff] [blame] | 3963 | // TODO: List Microsoft (IDL-style attribute) spellings once we fully | 
|  | 3964 | // support them. | 
| Richard Smith | 09ac4a1 | 2018-08-30 19:16:33 +0000 | [diff] [blame] | 3965 | if (K == SpellingKind::Microsoft) | 
|  | 3966 | continue; | 
|  | 3967 |  | 
|  | 3968 | bool PrintedAny = false; | 
|  | 3969 | for (StringRef Spelling : Doc.SupportedSpellings[K]) { | 
|  | 3970 | if (PrintedAny) | 
|  | 3971 | OS << " |br| "; | 
|  | 3972 | OS << "``" << Spelling << "``"; | 
|  | 3973 | PrintedAny = true; | 
|  | 3974 | } | 
|  | 3975 |  | 
|  | 3976 | OS << "\",\""; | 
|  | 3977 | } | 
|  | 3978 |  | 
|  | 3979 | if (getPragmaAttributeSupport(Records).isAttributedSupported( | 
|  | 3980 | *Doc.Attribute)) | 
|  | 3981 | OS << "Yes"; | 
| Tyler Nowicki | e8b07ed | 2014-06-13 17:57:25 +0000 | [diff] [blame] | 3982 | OS << "\"\n\n"; | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3983 |  | 
|  | 3984 | // If the attribute is deprecated, print a message about it, and possibly | 
|  | 3985 | // provide a replacement attribute. | 
| Aaron Ballman | 1a3e585 | 2014-02-17 16:18:32 +0000 | [diff] [blame] | 3986 | if (!Doc.Documentation->isValueUnset("Deprecated")) { | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3987 | OS << "This attribute has been deprecated, and may be removed in a future " | 
|  | 3988 | << "version of Clang."; | 
| Aaron Ballman | 1a3e585 | 2014-02-17 16:18:32 +0000 | [diff] [blame] | 3989 | const Record &Deprecated = *Doc.Documentation->getValueAsDef("Deprecated"); | 
| Erich Keane | 3bff414 | 2017-10-16 23:25:24 +0000 | [diff] [blame] | 3990 | const StringRef Replacement = Deprecated.getValueAsString("Replacement"); | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3991 | if (!Replacement.empty()) | 
| Joel E. Denny | 6053ec2 | 2018-02-28 16:57:33 +0000 | [diff] [blame] | 3992 | OS << "  This attribute has been superseded by ``" << Replacement | 
|  | 3993 | << "``."; | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3994 | OS << "\n\n"; | 
|  | 3995 | } | 
|  | 3996 |  | 
| Erich Keane | 3bff414 | 2017-10-16 23:25:24 +0000 | [diff] [blame] | 3997 | const StringRef ContentStr = Doc.Documentation->getValueAsString("Content"); | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 3998 | // Trim leading and trailing newlines and spaces. | 
| Erich Keane | 3bff414 | 2017-10-16 23:25:24 +0000 | [diff] [blame] | 3999 | OS << ContentStr.trim(); | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 4000 |  | 
|  | 4001 | OS << "\n\n\n"; | 
|  | 4002 | } | 
|  | 4003 |  | 
|  | 4004 | void EmitClangAttrDocs(RecordKeeper &Records, raw_ostream &OS) { | 
|  | 4005 | // Get the documentation introduction paragraph. | 
|  | 4006 | const Record *Documentation = Records.getDef("GlobalDocumentation"); | 
|  | 4007 | if (!Documentation) { | 
|  | 4008 | PrintFatalError("The Documentation top-level definition is missing, " | 
|  | 4009 | "no documentation will be generated."); | 
|  | 4010 | return; | 
|  | 4011 | } | 
|  | 4012 |  | 
| Aaron Ballman | 4de1b58 | 2014-02-19 22:59:32 +0000 | [diff] [blame] | 4013 | OS << Documentation->getValueAsString("Intro") << "\n"; | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 4014 |  | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 4015 | // Gather the Documentation lists from each of the attributes, based on the | 
|  | 4016 | // category provided. | 
|  | 4017 | std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 4018 | std::map<const Record *, std::vector<DocumentationData>> SplitDocs; | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 4019 | for (const auto *A : Attrs) { | 
|  | 4020 | const Record &Attr = *A; | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 4021 | std::vector<Record *> Docs = Attr.getValueAsListOfDefs("Documentation"); | 
| Aaron Ballman | 2f22b94 | 2014-05-20 19:47:14 +0000 | [diff] [blame] | 4022 | for (const auto *D : Docs) { | 
|  | 4023 | const Record &Doc = *D; | 
| Aaron Ballman | 4de1b58 | 2014-02-19 22:59:32 +0000 | [diff] [blame] | 4024 | const Record *Category = Doc.getValueAsDef("Category"); | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 4025 | // If the category is "undocumented", then there cannot be any other | 
|  | 4026 | // documentation categories (otherwise, the attribute would become | 
|  | 4027 | // documented). | 
| Erich Keane | 3bff414 | 2017-10-16 23:25:24 +0000 | [diff] [blame] | 4028 | const StringRef Cat = Category->getValueAsString("Name"); | 
| Aaron Ballman | 4de1b58 | 2014-02-19 22:59:32 +0000 | [diff] [blame] | 4029 | bool Undocumented = Cat == "Undocumented"; | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 4030 | if (Undocumented && Docs.size() > 1) | 
|  | 4031 | PrintFatalError(Doc.getLoc(), | 
|  | 4032 | "Attribute is \"Undocumented\", but has multiple " | 
| Erich Keane | a98a2be | 2017-10-16 20:31:05 +0000 | [diff] [blame] | 4033 | "documentation categories"); | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 4034 |  | 
|  | 4035 | if (!Undocumented) | 
| Erich Keane | a98a2be | 2017-10-16 20:31:05 +0000 | [diff] [blame] | 4036 | SplitDocs[Category].push_back(DocumentationData( | 
| Richard Smith | 09ac4a1 | 2018-08-30 19:16:33 +0000 | [diff] [blame] | 4037 | Doc, Attr, GetAttributeHeadingAndSpellings(Doc, Attr))); | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 4038 | } | 
|  | 4039 | } | 
|  | 4040 |  | 
|  | 4041 | // Having split the attributes out based on what documentation goes where, | 
|  | 4042 | // we can begin to generate sections of documentation. | 
| Erich Keane | a98a2be | 2017-10-16 20:31:05 +0000 | [diff] [blame] | 4043 | for (auto &I : SplitDocs) { | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 4044 | WriteCategoryHeader(I.first, OS); | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 4045 |  | 
| Fangrui Song | 1d38c13 | 2018-09-30 21:41:11 +0000 | [diff] [blame] | 4046 | llvm::sort(I.second, | 
| Mandeep Singh Grang | c205d8c | 2018-03-27 16:50:00 +0000 | [diff] [blame] | 4047 | [](const DocumentationData &D1, const DocumentationData &D2) { | 
|  | 4048 | return D1.Heading < D2.Heading; | 
| Fangrui Song | 1d38c13 | 2018-09-30 21:41:11 +0000 | [diff] [blame] | 4049 | }); | 
| Erich Keane | a98a2be | 2017-10-16 20:31:05 +0000 | [diff] [blame] | 4050 |  | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 4051 | // Walk over each of the attributes in the category and write out their | 
|  | 4052 | // documentation. | 
| Aaron Ballman | b097f7fe | 2014-03-02 17:38:37 +0000 | [diff] [blame] | 4053 | for (const auto &Doc : I.second) | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 4054 | WriteDocumentation(Records, Doc, OS); | 
|  | 4055 | } | 
|  | 4056 | } | 
|  | 4057 |  | 
|  | 4058 | void EmitTestPragmaAttributeSupportedAttributes(RecordKeeper &Records, | 
|  | 4059 | raw_ostream &OS) { | 
|  | 4060 | PragmaClangAttributeSupport Support = getPragmaAttributeSupport(Records); | 
|  | 4061 | ParsedAttrMap Attrs = getParsedAttrList(Records); | 
| Erik Pilkington | 23c48c2 | 2018-12-04 00:31:31 +0000 | [diff] [blame] | 4062 | OS << "#pragma clang attribute supports the following attributes:\n"; | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 4063 | for (const auto &I : Attrs) { | 
|  | 4064 | if (!Support.isAttributedSupported(*I.second)) | 
|  | 4065 | continue; | 
|  | 4066 | OS << I.first; | 
|  | 4067 | if (I.second->isValueUnset("Subjects")) { | 
|  | 4068 | OS << " ()\n"; | 
|  | 4069 | continue; | 
|  | 4070 | } | 
|  | 4071 | const Record *SubjectObj = I.second->getValueAsDef("Subjects"); | 
|  | 4072 | std::vector<Record *> Subjects = | 
|  | 4073 | SubjectObj->getValueAsListOfDefs("Subjects"); | 
|  | 4074 | OS << " ("; | 
|  | 4075 | for (const auto &Subject : llvm::enumerate(Subjects)) { | 
|  | 4076 | if (Subject.index()) | 
|  | 4077 | OS << ", "; | 
| Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 4078 | PragmaClangAttributeSupport::RuleOrAggregateRuleSet &RuleSet = | 
|  | 4079 | Support.SubjectsToRules.find(Subject.value())->getSecond(); | 
|  | 4080 | if (RuleSet.isRule()) { | 
|  | 4081 | OS << RuleSet.getRule().getEnumValueName(); | 
|  | 4082 | continue; | 
|  | 4083 | } | 
|  | 4084 | OS << "("; | 
|  | 4085 | for (const auto &Rule : llvm::enumerate(RuleSet.getAggregateRuleSet())) { | 
|  | 4086 | if (Rule.index()) | 
|  | 4087 | OS << ", "; | 
|  | 4088 | OS << Rule.value().getEnumValueName(); | 
|  | 4089 | } | 
|  | 4090 | OS << ")"; | 
| Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 4091 | } | 
|  | 4092 | OS << ")\n"; | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 4093 | } | 
| Erik Pilkington | 23c48c2 | 2018-12-04 00:31:31 +0000 | [diff] [blame] | 4094 | OS << "End of supported attributes.\n"; | 
| Aaron Ballman | 97dba04 | 2014-02-17 15:27:10 +0000 | [diff] [blame] | 4095 | } | 
|  | 4096 |  | 
| Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 4097 | } // end namespace clang |