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