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