| 1 |
//===-- ClangAttrEmitter.cpp - Generate Clang attribute handling ----------===// |
| 2 |
// |
| 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 |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
// These tablegen backends emit Clang attribute processing code |
| 10 |
// |
| 11 |
//===----------------------------------------------------------------------===// |
| 12 |
|
| 13 |
#include "TableGenBackends.h" |
| 14 |
#include "ASTTableGen.h" |
| 15 |
|
| 16 |
#include "llvm/ADT/ArrayRef.h" |
| 17 |
#include "llvm/ADT/DenseMap.h" |
| 18 |
#include "llvm/ADT/DenseSet.h" |
| 19 |
#include "llvm/ADT/MapVector.h" |
| 20 |
#include "llvm/ADT/STLExtras.h" |
| 21 |
#include "llvm/ADT/SmallString.h" |
| 22 |
#include "llvm/ADT/StringExtras.h" |
| 23 |
#include "llvm/ADT/StringMap.h" |
| 24 |
#include "llvm/ADT/StringRef.h" |
| 25 |
#include "llvm/ADT/StringSwitch.h" |
| 26 |
#include "llvm/Support/ErrorHandling.h" |
| 27 |
#include "llvm/Support/raw_ostream.h" |
| 28 |
#include "llvm/TableGen/Error.h" |
| 29 |
#include "llvm/TableGen/Record.h" |
| 30 |
#include "llvm/TableGen/StringMatcher.h" |
| 31 |
#include "llvm/TableGen/TableGenBackend.h" |
| 32 |
#include <cassert> |
| 33 |
#include <cctype> |
| 34 |
#include <cstddef> |
| 35 |
#include <cstdint> |
| 36 |
#include <map> |
| 37 |
#include <memory> |
| 38 |
#include <optional> |
| 39 |
#include <set> |
| 40 |
#include <string> |
| 41 |
#include <utility> |
| 42 |
#include <vector> |
| 43 |
|
| 44 |
using namespace llvm; |
| 45 |
|
| 46 |
namespace { |
| 47 |
|
| 48 |
class FlattenedSpelling { |
| 49 |
StringRef V, N, NS; |
| 50 |
bool K = false; |
| 51 |
const Record &OriginalSpelling; |
| 52 |
|
| 53 |
public: |
| 54 |
FlattenedSpelling(StringRef Variety, StringRef Name, StringRef Namespace, |
| 55 |
bool KnownToGCC, const Record &OriginalSpelling) |
| 56 |
: V(Variety), N(Name), NS(Namespace), K(KnownToGCC), |
| 57 |
OriginalSpelling(OriginalSpelling) {} |
| 58 |
explicit FlattenedSpelling(const Record &Spelling) |
| 59 |
: V(Spelling.getValueAsString("Variety")), |
| 60 |
N(Spelling.getValueAsString("Name")), OriginalSpelling(Spelling) { |
| 61 |
assert(V != "GCC" && V != "Clang" && |
| 62 |
"Given a GCC spelling, which means this hasn't been flattened!"); |
| 63 |
if (V == "CXX11" || V == "C23" || V == "Pragma") |
| 64 |
NS = Spelling.getValueAsString("Namespace"); |
| 65 |
} |
| 66 |
|
| 67 |
StringRef variety() const { return V; } |
| 68 |
StringRef name() const { return N; } |
| 69 |
StringRef nameSpace() const { return NS; } |
| 70 |
bool knownToGCC() const { return K; } |
| 71 |
const Record &getSpellingRecord() const { return OriginalSpelling; } |
| 72 |
}; |
| 73 |
|
| 74 |
struct FlattenedSpellingInfo { |
| 75 |
FlattenedSpellingInfo(StringRef Syntax, StringRef Scope, |
| 76 |
const std::string &TargetTest, uint32_t ArgMask) |
| 77 |
: Syntax(Syntax), Scope(Scope), TargetTest(TargetTest), ArgMask(ArgMask) { |
| 78 |
} |
| 79 |
StringRef Syntax; |
| 80 |
StringRef Scope; |
| 81 |
std::string TargetTest; |
| 82 |
uint32_t ArgMask; |
| 83 |
}; |
| 84 |
using FSIVecTy = std::vector<FlattenedSpellingInfo>; |
| 85 |
|
| 86 |
} // end anonymous namespace |
| 87 |
|
| 88 |
static bool GenerateTargetSpecificAttrChecks(const Record *R, |
| 89 |
std::vector<StringRef> &Arches, |
| 90 |
std::string &Test, |
| 91 |
std::string *FnName); |
| 92 |
static bool isStringLiteralArgument(const Record *Arg); |
| 93 |
static bool isVariadicStringLiteralArgument(const Record *Arg); |
| 94 |
|
| 95 |
static std::vector<FlattenedSpelling> |
| 96 |
GetFlattenedSpellings(const Record &Attr) { |
| 97 |
std::vector<FlattenedSpelling> Ret; |
| 98 |
|
| 99 |
for (const auto &Spelling : Attr.getValueAsListOfDefs("Spellings")) { |
| 100 |
StringRef Variety = Spelling->getValueAsString("Variety"); |
| 101 |
StringRef Name = Spelling->getValueAsString("Name"); |
| 102 |
if (Variety == "GCC") { |
| 103 |
Ret.emplace_back("GNU", Name, "", true, *Spelling); |
| 104 |
Ret.emplace_back("CXX11", Name, "gnu", true, *Spelling); |
| 105 |
if (Spelling->getValueAsBit("AllowInC")) |
| 106 |
Ret.emplace_back("C23", Name, "gnu", true, *Spelling); |
| 107 |
} else if (Variety == "Clang") { |
| 108 |
Ret.emplace_back("GNU", Name, "", false, *Spelling); |
| 109 |
Ret.emplace_back("CXX11", Name, "clang", false, *Spelling); |
| 110 |
if (Spelling->getValueAsBit("AllowInC")) |
| 111 |
Ret.emplace_back("C23", Name, "clang", false, *Spelling); |
| 112 |
} else if (Variety == "ClangGCC") { |
| 113 |
Ret.emplace_back("GNU", Name, "", false, *Spelling); |
| 114 |
Ret.emplace_back("CXX11", Name, "clang", false, *Spelling); |
| 115 |
Ret.emplace_back("CXX11", Name, "gnu", false, *Spelling); |
| 116 |
if (Spelling->getValueAsBit("AllowInC")) { |
| 117 |
Ret.emplace_back("C23", Name, "clang", false, *Spelling); |
| 118 |
Ret.emplace_back("C23", Name, "gnu", false, *Spelling); |
| 119 |
} |
| 120 |
} else { |
| 121 |
Ret.push_back(FlattenedSpelling(*Spelling)); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
return Ret; |
| 126 |
} |
| 127 |
|
| 128 |
static std::string ReadPCHRecord(StringRef type) { |
| 129 |
return StringSwitch<std::string>(type) |
| 130 |
.EndsWith("Decl *", "Record.readDeclAs<" + type.drop_back().str() + ">()") |
| 131 |
.Case("TypeSourceInfo *", "Record.readTypeSourceInfo()") |
| 132 |
.Case("Expr *", "Record.readExpr()") |
| 133 |
.Case("const IdentifierInfo *", "Record.readIdentifier()") |
| 134 |
.Case("StringRef", "Record.readString()") |
| 135 |
.Case("ParamIdx", "ParamIdx::deserialize(Record.readInt())") |
| 136 |
.Case("OMPTraitInfo *", "Record.readOMPTraitInfo()") |
| 137 |
.Default("Record.readInt()"); |
| 138 |
} |
| 139 |
|
| 140 |
// Get a type that is suitable for storing an object of the specified type. |
| 141 |
static StringRef getStorageType(StringRef type) { |
| 142 |
return StringSwitch<StringRef>(type) |
| 143 |
.Case("StringRef", "std::string") |
| 144 |
.Default(type); |
| 145 |
} |
| 146 |
|
| 147 |
// Assumes that the way to get the value is SA->getname() |
| 148 |
static std::string WritePCHRecord(StringRef type, StringRef name) { |
| 149 |
return "Record." + |
| 150 |
StringSwitch<std::string>(type) |
| 151 |
.EndsWith("Decl *", "AddDeclRef(" + name.str() + ");\n") |
| 152 |
.Case("TypeSourceInfo *", |
| 153 |
"AddTypeSourceInfo(" + name.str() + ");\n") |
| 154 |
.Case("Expr *", "AddStmt(" + name.str() + ");\n") |
| 155 |
.Case("const IdentifierInfo *", |
| 156 |
"AddIdentifierRef(" + name.str() + ");\n") |
| 157 |
.Case("StringRef", "AddString(" + name.str() + ");\n") |
| 158 |
.Case("ParamIdx", "push_back(" + name.str() + ".serialize());\n") |
| 159 |
.Case("OMPTraitInfo *", "writeOMPTraitInfo(" + name.str() + ");\n") |
| 160 |
.Default("push_back(" + name.str() + ");\n"); |
| 161 |
} |
| 162 |
|
| 163 |
// Normalize attribute name by removing leading and trailing |
| 164 |
// underscores. For example, __foo, foo__, __foo__ would |
| 165 |
// become foo. |
| 166 |
static StringRef NormalizeAttrName(StringRef AttrName) { |
| 167 |
AttrName.consume_front("__"); |
| 168 |
AttrName.consume_back("__"); |
| 169 |
return AttrName; |
| 170 |
} |
| 171 |
|
| 172 |
// Normalize the name by removing any and all leading and trailing underscores. |
| 173 |
// This is different from NormalizeAttrName in that it also handles names like |
| 174 |
// _pascal and __pascal. |
| 175 |
static StringRef NormalizeNameForSpellingComparison(StringRef Name) { |
| 176 |
return Name.trim("_"); |
| 177 |
} |
| 178 |
|
| 179 |
// Normalize the spelling of a GNU attribute (i.e. "x" in "__attribute__((x))"), |
| 180 |
// removing "__" if it appears at the beginning and end of the attribute's name. |
| 181 |
static StringRef NormalizeGNUAttrSpelling(StringRef AttrSpelling) { |
| 182 |
if (AttrSpelling.starts_with("__") && AttrSpelling.ends_with("__")) { |
| 183 |
AttrSpelling = AttrSpelling.substr(2, AttrSpelling.size() - 4); |
| 184 |
} |
| 185 |
|
| 186 |
return AttrSpelling; |
| 187 |
} |
| 188 |
|
| 189 |
typedef std::vector<std::pair<std::string, const Record *>> ParsedAttrMap; |
| 190 |
|
| 191 |
static ParsedAttrMap getParsedAttrList(const RecordKeeper &Records, |
| 192 |
ParsedAttrMap *Dupes = nullptr, |
| 193 |
bool SemaOnly = true) { |
| 194 |
std::set<std::string> Seen; |
| 195 |
ParsedAttrMap R; |
| 196 |
for (const Record *Attr : Records.getAllDerivedDefinitions("Attr")) { |
| 197 |
if (!SemaOnly || Attr->getValueAsBit("SemaHandler")) { |
| 198 |
std::string AN; |
| 199 |
if (Attr->isSubClassOf("TargetSpecificAttr") && |
| 200 |
!Attr->isValueUnset("ParseKind")) { |
| 201 |
AN = Attr->getValueAsString("ParseKind").str(); |
| 202 |
|
| 203 |
// If this attribute has already been handled, it does not need to be |
| 204 |
// handled again. |
| 205 |
if (!Seen.insert(AN).second) { |
| 206 |
if (Dupes) |
| 207 |
Dupes->push_back(std::make_pair(AN, Attr)); |
| 208 |
continue; |
| 209 |
} |
| 210 |
} else |
| 211 |
AN = NormalizeAttrName(Attr->getName()).str(); |
| 212 |
|
| 213 |
R.push_back(std::make_pair(AN, Attr)); |
| 214 |
} |
| 215 |
} |
| 216 |
return R; |
| 217 |
} |
| 218 |
|
| 219 |
namespace { |
| 220 |
|
| 221 |
class Argument { |
| 222 |
std::string lowerName, upperName; |
| 223 |
StringRef attrName; |
| 224 |
bool isOpt; |
| 225 |
bool Fake; |
| 226 |
|
| 227 |
public: |
| 228 |
Argument(StringRef Arg, StringRef Attr) |
| 229 |
: lowerName(Arg.str()), upperName(lowerName), attrName(Attr), |
| 230 |
isOpt(false), Fake(false) { |
| 231 |
if (!lowerName.empty()) { |
| 232 |
lowerName[0] = std::tolower(lowerName[0]); |
| 233 |
upperName[0] = std::toupper(upperName[0]); |
| 234 |
} |
| 235 |
// Work around MinGW's macro definition of 'interface' to 'struct'. We |
| 236 |
// have an attribute argument called 'Interface', so only the lower case |
| 237 |
// name conflicts with the macro definition. |
| 238 |
if (lowerName == "interface") |
| 239 |
lowerName = "interface_"; |
| 240 |
} |
| 241 |
Argument(const Record &Arg, StringRef Attr) |
| 242 |
: Argument(Arg.getValueAsString("Name"), Attr) {} |
| 243 |
virtual ~Argument() = default; |
| 244 |
|
| 245 |
StringRef getLowerName() const { return lowerName; } |
| 246 |
StringRef getUpperName() const { return upperName; } |
| 247 |
StringRef getAttrName() const { return attrName; } |
| 248 |
|
| 249 |
bool isOptional() const { return isOpt; } |
| 250 |
void setOptional(bool set) { isOpt = set; } |
| 251 |
|
| 252 |
bool isFake() const { return Fake; } |
| 253 |
void setFake(bool fake) { Fake = fake; } |
| 254 |
|
| 255 |
// These functions print the argument contents formatted in different ways. |
| 256 |
virtual void writeAccessors(raw_ostream &OS) const = 0; |
| 257 |
virtual void writeAccessorDefinitions(raw_ostream &OS) const {} |
| 258 |
virtual void writeASTVisitorTraversal(raw_ostream &OS) const {} |
| 259 |
virtual void writeCloneArgs(raw_ostream &OS) const = 0; |
| 260 |
virtual void writeTemplateInstantiationArgs(raw_ostream &OS) const = 0; |
| 261 |
virtual void writeTemplateInstantiation(raw_ostream &OS) const {} |
| 262 |
virtual void writeCtorBody(raw_ostream &OS) const {} |
| 263 |
virtual void writeCtorInitializers(raw_ostream &OS) const = 0; |
| 264 |
virtual void writeCtorDefaultInitializers(raw_ostream &OS) const = 0; |
| 265 |
virtual void writeCtorParameters(raw_ostream &OS) const = 0; |
| 266 |
virtual void writeDeclarations(raw_ostream &OS) const = 0; |
| 267 |
virtual void writePCHReadArgs(raw_ostream &OS) const = 0; |
| 268 |
virtual void writePCHReadDecls(raw_ostream &OS) const = 0; |
| 269 |
virtual void writePCHWrite(raw_ostream &OS) const = 0; |
| 270 |
virtual std::string getIsOmitted() const { return "false"; } |
| 271 |
virtual void writeValue(raw_ostream &OS) const = 0; |
| 272 |
virtual void writeDump(raw_ostream &OS) const = 0; |
| 273 |
virtual void writeDumpChildren(raw_ostream &OS) const {} |
| 274 |
virtual void writeHasChildren(raw_ostream &OS) const { OS << "false"; } |
| 275 |
|
| 276 |
virtual bool isEnumArg() const { return false; } |
| 277 |
virtual bool isVariadicEnumArg() const { return false; } |
| 278 |
virtual bool isVariadic() const { return false; } |
| 279 |
|
| 280 |
virtual void writeImplicitCtorArgs(raw_ostream &OS) const { |
| 281 |
OS << getUpperName(); |
| 282 |
} |
| 283 |
}; |
| 284 |
|
| 285 |
class SimpleArgument : public Argument { |
| 286 |
std::string type; |
| 287 |
|
| 288 |
public: |
| 289 |
SimpleArgument(const Record &Arg, StringRef Attr, std::string T) |
| 290 |
: Argument(Arg, Attr), type(std::move(T)) {} |
| 291 |
|
| 292 |
std::string getType() const { return type; } |
| 293 |
|
| 294 |
void writeAccessors(raw_ostream &OS) const override { |
| 295 |
OS << " " << type << " get" << getUpperName() << "() const {\n"; |
| 296 |
OS << " return " << getLowerName() << ";\n"; |
| 297 |
OS << " }"; |
| 298 |
} |
| 299 |
|
| 300 |
void writeCloneArgs(raw_ostream &OS) const override { |
| 301 |
OS << getLowerName(); |
| 302 |
} |
| 303 |
|
| 304 |
void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
| 305 |
OS << "A->get" << getUpperName() << "()"; |
| 306 |
} |
| 307 |
|
| 308 |
void writeCtorInitializers(raw_ostream &OS) const override { |
| 309 |
OS << getLowerName() << "(" << getUpperName() << ")"; |
| 310 |
} |
| 311 |
|
| 312 |
void writeCtorDefaultInitializers(raw_ostream &OS) const override { |
| 313 |
OS << getLowerName() << "()"; |
| 314 |
} |
| 315 |
|
| 316 |
void writeCtorParameters(raw_ostream &OS) const override { |
| 317 |
OS << type << " " << getUpperName(); |
| 318 |
} |
| 319 |
|
| 320 |
void writeDeclarations(raw_ostream &OS) const override { |
| 321 |
OS << type << " " << getLowerName() << ";"; |
| 322 |
} |
| 323 |
|
| 324 |
void writePCHReadDecls(raw_ostream &OS) const override { |
| 325 |
std::string read = ReadPCHRecord(type); |
| 326 |
OS << " " << type << " " << getLowerName() << " = " << read << ";\n"; |
| 327 |
} |
| 328 |
|
| 329 |
void writePCHReadArgs(raw_ostream &OS) const override { |
| 330 |
OS << getLowerName(); |
| 331 |
} |
| 332 |
|
| 333 |
void writePCHWrite(raw_ostream &OS) const override { |
| 334 |
OS << " " |
| 335 |
<< WritePCHRecord(type, "SA->get" + getUpperName().str() + "()"); |
| 336 |
} |
| 337 |
|
| 338 |
std::string getIsOmitted() const override { |
| 339 |
auto IsOneOf = [](StringRef subject, auto... list) { |
| 340 |
return ((subject == list) || ...); |
| 341 |
}; |
| 342 |
|
| 343 |
if (IsOneOf(type, "const IdentifierInfo *", "Expr *")) |
| 344 |
return "!get" + getUpperName().str() + "()"; |
| 345 |
if (IsOneOf(type, "TypeSourceInfo *")) |
| 346 |
return "!get" + getUpperName().str() + "Loc()"; |
| 347 |
if (IsOneOf(type, "ParamIdx")) |
| 348 |
return "!get" + getUpperName().str() + "().isValid()"; |
| 349 |
|
| 350 |
assert(IsOneOf(type, "unsigned", "int", "bool", "FunctionDecl *", |
| 351 |
"VarDecl *")); |
| 352 |
return "false"; |
| 353 |
} |
| 354 |
|
| 355 |
void writeValue(raw_ostream &OS) const override { |
| 356 |
if (type == "FunctionDecl *") |
| 357 |
OS << "\" << get" << getUpperName() |
| 358 |
<< "()->getNameInfo().getAsString() << \""; |
| 359 |
else if (type == "const IdentifierInfo *") |
| 360 |
// Some non-optional (comma required) identifier arguments can be the |
| 361 |
// empty string but are then recorded as a nullptr. |
| 362 |
OS << "\" << (get" << getUpperName() << "() ? get" << getUpperName() |
| 363 |
<< "()->getName() : \"\") << \""; |
| 364 |
else if (type == "VarDecl *") |
| 365 |
OS << "\" << get" << getUpperName() << "()->getName() << \""; |
| 366 |
else if (type == "TypeSourceInfo *") |
| 367 |
OS << "\" << get" << getUpperName() << "().getAsString() << \""; |
| 368 |
else if (type == "ParamIdx") |
| 369 |
OS << "\" << get" << getUpperName() << "().getSourceIndex() << \""; |
| 370 |
else |
| 371 |
OS << "\" << get" << getUpperName() << "() << \""; |
| 372 |
} |
| 373 |
|
| 374 |
void writeDump(raw_ostream &OS) const override { |
| 375 |
if (StringRef(type).ends_with("Decl *")) { |
| 376 |
OS << " OS << \" \";\n"; |
| 377 |
OS << " dumpBareDeclRef(SA->get" << getUpperName() << "());\n"; |
| 378 |
} else if (type == "const IdentifierInfo *") { |
| 379 |
// Some non-optional (comma required) identifier arguments can be the |
| 380 |
// empty string but are then recorded as a nullptr. |
| 381 |
OS << " if (SA->get" << getUpperName() << "())\n" |
| 382 |
<< " OS << \" \" << SA->get" << getUpperName() |
| 383 |
<< "()->getName();\n"; |
| 384 |
} else if (type == "TypeSourceInfo *") { |
| 385 |
if (isOptional()) |
| 386 |
OS << " if (SA->get" << getUpperName() << "Loc())"; |
| 387 |
OS << " OS << \" \" << SA->get" << getUpperName() |
| 388 |
<< "().getAsString();\n"; |
| 389 |
} else if (type == "bool") { |
| 390 |
OS << " if (SA->get" << getUpperName() << "()) OS << \" " |
| 391 |
<< getUpperName() << "\";\n"; |
| 392 |
} else if (type == "int" || type == "unsigned") { |
| 393 |
OS << " OS << \" \" << SA->get" << getUpperName() << "();\n"; |
| 394 |
} else if (type == "ParamIdx") { |
| 395 |
if (isOptional()) |
| 396 |
OS << " if (SA->get" << getUpperName() << "().isValid())\n "; |
| 397 |
OS << " OS << \" \" << SA->get" << getUpperName() |
| 398 |
<< "().getSourceIndex();\n"; |
| 399 |
} else if (type == "OMPTraitInfo *") { |
| 400 |
OS << " OS << \" \" << SA->get" << getUpperName() << "();\n"; |
| 401 |
} else { |
| 402 |
llvm_unreachable("Unknown SimpleArgument type!"); |
| 403 |
} |
| 404 |
} |
| 405 |
}; |
| 406 |
|
| 407 |
class DefaultSimpleArgument : public SimpleArgument { |
| 408 |
int64_t Default; |
| 409 |
|
| 410 |
public: |
| 411 |
DefaultSimpleArgument(const Record &Arg, StringRef Attr, |
| 412 |
std::string T, int64_t Default) |
| 413 |
: SimpleArgument(Arg, Attr, T), Default(Default) {} |
| 414 |
|
| 415 |
void writeAccessors(raw_ostream &OS) const override { |
| 416 |
SimpleArgument::writeAccessors(OS); |
| 417 |
|
| 418 |
OS << "\n\n static const " << getType() << " Default" << getUpperName() |
| 419 |
<< " = "; |
| 420 |
if (getType() == "bool") |
| 421 |
OS << (Default != 0 ? "true" : "false"); |
| 422 |
else |
| 423 |
OS << Default; |
| 424 |
OS << ";"; |
| 425 |
} |
| 426 |
}; |
| 427 |
|
| 428 |
class StringArgument : public Argument { |
| 429 |
public: |
| 430 |
StringArgument(const Record &Arg, StringRef Attr) |
| 431 |
: Argument(Arg, Attr) |
| 432 |
{} |
| 433 |
|
| 434 |
void writeAccessors(raw_ostream &OS) const override { |
| 435 |
OS << " llvm::StringRef get" << getUpperName() << "() const {\n"; |
| 436 |
OS << " return llvm::StringRef(" << getLowerName() << ", " |
| 437 |
<< getLowerName() << "Length);\n"; |
| 438 |
OS << " }\n"; |
| 439 |
OS << " unsigned get" << getUpperName() << "Length() const {\n"; |
| 440 |
OS << " return " << getLowerName() << "Length;\n"; |
| 441 |
OS << " }\n"; |
| 442 |
OS << " void set" << getUpperName() |
| 443 |
<< "(ASTContext &C, llvm::StringRef S) {\n"; |
| 444 |
OS << " " << getLowerName() << "Length = S.size();\n"; |
| 445 |
OS << " this->" << getLowerName() << " = new (C, 1) char [" |
| 446 |
<< getLowerName() << "Length];\n"; |
| 447 |
OS << " if (!S.empty())\n"; |
| 448 |
OS << " std::memcpy(this->" << getLowerName() << ", S.data(), " |
| 449 |
<< getLowerName() << "Length);\n"; |
| 450 |
OS << " }"; |
| 451 |
} |
| 452 |
|
| 453 |
void writeCloneArgs(raw_ostream &OS) const override { |
| 454 |
OS << "get" << getUpperName() << "()"; |
| 455 |
} |
| 456 |
|
| 457 |
void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
| 458 |
OS << "A->get" << getUpperName() << "()"; |
| 459 |
} |
| 460 |
|
| 461 |
void writeCtorBody(raw_ostream &OS) const override { |
| 462 |
OS << " if (!" << getUpperName() << ".empty())\n"; |
| 463 |
OS << " std::memcpy(" << getLowerName() << ", " << getUpperName() |
| 464 |
<< ".data(), " << getLowerName() << "Length);\n"; |
| 465 |
} |
| 466 |
|
| 467 |
void writeCtorInitializers(raw_ostream &OS) const override { |
| 468 |
OS << getLowerName() << "Length(" << getUpperName() << ".size())," |
| 469 |
<< getLowerName() << "(new (Ctx, 1) char[" << getLowerName() |
| 470 |
<< "Length])"; |
| 471 |
} |
| 472 |
|
| 473 |
void writeCtorDefaultInitializers(raw_ostream &OS) const override { |
| 474 |
OS << getLowerName() << "Length(0)," << getLowerName() << "(nullptr)"; |
| 475 |
} |
| 476 |
|
| 477 |
void writeCtorParameters(raw_ostream &OS) const override { |
| 478 |
OS << "llvm::StringRef " << getUpperName(); |
| 479 |
} |
| 480 |
|
| 481 |
void writeDeclarations(raw_ostream &OS) const override { |
| 482 |
OS << "unsigned " << getLowerName() << "Length;\n"; |
| 483 |
OS << "char *" << getLowerName() << ";"; |
| 484 |
} |
| 485 |
|
| 486 |
void writePCHReadDecls(raw_ostream &OS) const override { |
| 487 |
OS << " std::string " << getLowerName() |
| 488 |
<< "= Record.readString();\n"; |
| 489 |
} |
| 490 |
|
| 491 |
void writePCHReadArgs(raw_ostream &OS) const override { |
| 492 |
OS << getLowerName(); |
| 493 |
} |
| 494 |
|
| 495 |
void writePCHWrite(raw_ostream &OS) const override { |
| 496 |
OS << " Record.AddString(SA->get" << getUpperName() << "());\n"; |
| 497 |
} |
| 498 |
|
| 499 |
void writeValue(raw_ostream &OS) const override { |
| 500 |
OS << "\\\"\" << get" << getUpperName() << "() << \"\\\""; |
| 501 |
} |
| 502 |
|
| 503 |
void writeDump(raw_ostream &OS) const override { |
| 504 |
OS << " OS << \" \\\"\" << SA->get" << getUpperName() |
| 505 |
<< "() << \"\\\"\";\n"; |
| 506 |
} |
| 507 |
}; |
| 508 |
|
| 509 |
class AlignedArgument : public Argument { |
| 510 |
public: |
| 511 |
AlignedArgument(const Record &Arg, StringRef Attr) |
| 512 |
: Argument(Arg, Attr) |
| 513 |
{} |
| 514 |
|
| 515 |
void writeAccessors(raw_ostream &OS) const override { |
| 516 |
OS << " bool is" << getUpperName() << "Dependent() const;\n"; |
| 517 |
OS << " bool is" << getUpperName() << "ErrorDependent() const;\n"; |
| 518 |
|
| 519 |
OS << " unsigned get" << getUpperName() << "(ASTContext &Ctx) const;\n"; |
| 520 |
|
| 521 |
OS << " bool is" << getUpperName() << "Expr() const {\n"; |
| 522 |
OS << " return is" << getLowerName() << "Expr;\n"; |
| 523 |
OS << " }\n"; |
| 524 |
|
| 525 |
OS << " Expr *get" << getUpperName() << "Expr() const {\n"; |
| 526 |
OS << " assert(is" << getLowerName() << "Expr);\n"; |
| 527 |
OS << " return " << getLowerName() << "Expr;\n"; |
| 528 |
OS << " }\n"; |
| 529 |
|
| 530 |
OS << " TypeSourceInfo *get" << getUpperName() << "Type() const {\n"; |
| 531 |
OS << " assert(!is" << getLowerName() << "Expr);\n"; |
| 532 |
OS << " return " << getLowerName() << "Type;\n"; |
| 533 |
OS << " }"; |
| 534 |
|
| 535 |
OS << " std::optional<unsigned> getCached" << getUpperName() |
| 536 |
<< "Value() const {\n"; |
| 537 |
OS << " return " << getLowerName() << "Cache;\n"; |
| 538 |
OS << " }"; |
| 539 |
|
| 540 |
OS << " void setCached" << getUpperName() |
| 541 |
<< "Value(unsigned AlignVal) {\n"; |
| 542 |
OS << " " << getLowerName() << "Cache = AlignVal;\n"; |
| 543 |
OS << " }"; |
| 544 |
} |
| 545 |
|
| 546 |
void writeAccessorDefinitions(raw_ostream &OS) const override { |
| 547 |
OS << "bool " << getAttrName() << "Attr::is" << getUpperName() |
| 548 |
<< "Dependent() const {\n"; |
| 549 |
OS << " if (is" << getLowerName() << "Expr)\n"; |
| 550 |
OS << " return " << getLowerName() << "Expr && (" << getLowerName() |
| 551 |
<< "Expr->isValueDependent() || " << getLowerName() |
| 552 |
<< "Expr->isTypeDependent());\n"; |
| 553 |
OS << " else\n"; |
| 554 |
OS << " return " << getLowerName() |
| 555 |
<< "Type->getType()->isDependentType();\n"; |
| 556 |
OS << "}\n"; |
| 557 |
|
| 558 |
OS << "bool " << getAttrName() << "Attr::is" << getUpperName() |
| 559 |
<< "ErrorDependent() const {\n"; |
| 560 |
OS << " if (is" << getLowerName() << "Expr)\n"; |
| 561 |
OS << " return " << getLowerName() << "Expr && " << getLowerName() |
| 562 |
<< "Expr->containsErrors();\n"; |
| 563 |
OS << " return " << getLowerName() |
| 564 |
<< "Type->getType()->containsErrors();\n"; |
| 565 |
OS << "}\n"; |
| 566 |
} |
| 567 |
|
| 568 |
void writeASTVisitorTraversal(raw_ostream &OS) const override { |
| 569 |
StringRef Name = getUpperName(); |
| 570 |
OS << " if (A->is" << Name << "Expr()) {\n" |
| 571 |
<< " if (!getDerived().TraverseStmt(A->get" << Name << "Expr()))\n" |
| 572 |
<< " return false;\n" |
| 573 |
<< " } else if (auto *TSI = A->get" << Name << "Type()) {\n" |
| 574 |
<< " if (!getDerived().TraverseTypeLoc(TSI->getTypeLoc()))\n" |
| 575 |
<< " return false;\n" |
| 576 |
<< " }\n"; |
| 577 |
} |
| 578 |
|
| 579 |
void writeCloneArgs(raw_ostream &OS) const override { |
| 580 |
OS << "is" << getLowerName() << "Expr, is" << getLowerName() |
| 581 |
<< "Expr ? static_cast<void*>(" << getLowerName() |
| 582 |
<< "Expr) : " << getLowerName() |
| 583 |
<< "Type"; |
| 584 |
} |
| 585 |
|
| 586 |
void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
| 587 |
// FIXME: move the definition in Sema::InstantiateAttrs to here. |
| 588 |
// In the meantime, aligned attributes are cloned. |
| 589 |
} |
| 590 |
|
| 591 |
void writeCtorBody(raw_ostream &OS) const override { |
| 592 |
OS << " if (is" << getLowerName() << "Expr)\n"; |
| 593 |
OS << " " << getLowerName() << "Expr = reinterpret_cast<Expr *>(" |
| 594 |
<< getUpperName() << ");\n"; |
| 595 |
OS << " else\n"; |
| 596 |
OS << " " << getLowerName() |
| 597 |
<< "Type = reinterpret_cast<TypeSourceInfo *>(" << getUpperName() |
| 598 |
<< ");\n"; |
| 599 |
} |
| 600 |
|
| 601 |
void writeCtorInitializers(raw_ostream &OS) const override { |
| 602 |
OS << "is" << getLowerName() << "Expr(Is" << getUpperName() << "Expr)"; |
| 603 |
} |
| 604 |
|
| 605 |
void writeCtorDefaultInitializers(raw_ostream &OS) const override { |
| 606 |
OS << "is" << getLowerName() << "Expr(false)"; |
| 607 |
} |
| 608 |
|
| 609 |
void writeCtorParameters(raw_ostream &OS) const override { |
| 610 |
OS << "bool Is" << getUpperName() << "Expr, void *" << getUpperName(); |
| 611 |
} |
| 612 |
|
| 613 |
void writeImplicitCtorArgs(raw_ostream &OS) const override { |
| 614 |
OS << "Is" << getUpperName() << "Expr, " << getUpperName(); |
| 615 |
} |
| 616 |
|
| 617 |
void writeDeclarations(raw_ostream &OS) const override { |
| 618 |
OS << "bool is" << getLowerName() << "Expr;\n"; |
| 619 |
OS << "union {\n"; |
| 620 |
OS << "Expr *" << getLowerName() << "Expr;\n"; |
| 621 |
OS << "TypeSourceInfo *" << getLowerName() << "Type;\n"; |
| 622 |
OS << "};\n"; |
| 623 |
OS << "std::optional<unsigned> " << getLowerName() << "Cache;\n"; |
| 624 |
} |
| 625 |
|
| 626 |
void writePCHReadArgs(raw_ostream &OS) const override { |
| 627 |
OS << "is" << getLowerName() << "Expr, " << getLowerName() << "Ptr"; |
| 628 |
} |
| 629 |
|
| 630 |
void writePCHReadDecls(raw_ostream &OS) const override { |
| 631 |
OS << " bool is" << getLowerName() << "Expr = Record.readInt();\n"; |
| 632 |
OS << " void *" << getLowerName() << "Ptr;\n"; |
| 633 |
OS << " if (is" << getLowerName() << "Expr)\n"; |
| 634 |
OS << " " << getLowerName() << "Ptr = Record.readExpr();\n"; |
| 635 |
OS << " else\n"; |
| 636 |
OS << " " << getLowerName() |
| 637 |
<< "Ptr = Record.readTypeSourceInfo();\n"; |
| 638 |
} |
| 639 |
|
| 640 |
void writePCHWrite(raw_ostream &OS) const override { |
| 641 |
OS << " Record.push_back(SA->is" << getUpperName() << "Expr());\n"; |
| 642 |
OS << " if (SA->is" << getUpperName() << "Expr())\n"; |
| 643 |
OS << " Record.AddStmt(SA->get" << getUpperName() << "Expr());\n"; |
| 644 |
OS << " else\n"; |
| 645 |
OS << " Record.AddTypeSourceInfo(SA->get" << getUpperName() |
| 646 |
<< "Type());\n"; |
| 647 |
} |
| 648 |
|
| 649 |
std::string getIsOmitted() const override { |
| 650 |
return "!((is" + getLowerName().str() + "Expr && " + |
| 651 |
getLowerName().str() + "Expr) || (!is" + getLowerName().str() + |
| 652 |
"Expr && " + getLowerName().str() + "Type))"; |
| 653 |
} |
| 654 |
|
| 655 |
void writeValue(raw_ostream &OS) const override { |
| 656 |
OS << "\";\n"; |
| 657 |
OS << " if (is" << getLowerName() << "Expr && " << getLowerName() |
| 658 |
<< "Expr)"; |
| 659 |
OS << " " << getLowerName() |
| 660 |
<< "Expr->printPretty(OS, nullptr, Policy);\n"; |
| 661 |
OS << " if (!is" << getLowerName() << "Expr && " << getLowerName() |
| 662 |
<< "Type)"; |
| 663 |
OS << " " << getLowerName() |
| 664 |
<< "Type->getType().print(OS, Policy);\n"; |
| 665 |
OS << " OS << \""; |
| 666 |
} |
| 667 |
|
| 668 |
void writeDump(raw_ostream &OS) const override { |
| 669 |
OS << " if (!SA->is" << getUpperName() << "Expr())\n"; |
| 670 |
OS << " dumpType(SA->get" << getUpperName() |
| 671 |
<< "Type()->getType());\n"; |
| 672 |
} |
| 673 |
|
| 674 |
void writeDumpChildren(raw_ostream &OS) const override { |
| 675 |
OS << " if (SA->is" << getUpperName() << "Expr())\n"; |
| 676 |
OS << " Visit(SA->get" << getUpperName() << "Expr());\n"; |
| 677 |
} |
| 678 |
|
| 679 |
void writeHasChildren(raw_ostream &OS) const override { |
| 680 |
OS << "SA->is" << getUpperName() << "Expr()"; |
| 681 |
} |
| 682 |
}; |
| 683 |
|
| 684 |
class VariadicArgument : public Argument { |
| 685 |
std::string Type, ArgName, ArgSizeName, RangeName; |
| 686 |
|
| 687 |
protected: |
| 688 |
// Assumed to receive a parameter: raw_ostream OS. |
| 689 |
virtual void writeValueImpl(raw_ostream &OS) const { |
| 690 |
OS << " OS << Val;\n"; |
| 691 |
} |
| 692 |
// Assumed to receive a parameter: raw_ostream OS. |
| 693 |
virtual void writeDumpImpl(raw_ostream &OS) const { |
| 694 |
OS << " OS << \" \" << Val;\n"; |
| 695 |
} |
| 696 |
|
| 697 |
public: |
| 698 |
VariadicArgument(const Record &Arg, StringRef Attr, std::string T) |
| 699 |
: Argument(Arg, Attr), Type(std::move(T)), |
| 700 |
ArgName(getLowerName().str() + "_"), ArgSizeName(ArgName + "Size"), |
| 701 |
RangeName(getLowerName().str()) {} |
| 702 |
|
| 703 |
VariadicArgument(StringRef Arg, StringRef Attr, std::string T) |
| 704 |
: Argument(Arg, Attr), Type(std::move(T)), |
| 705 |
ArgName(getLowerName().str() + "_"), ArgSizeName(ArgName + "Size"), |
| 706 |
RangeName(getLowerName().str()) {} |
| 707 |
|
| 708 |
const std::string &getType() const { return Type; } |
| 709 |
const std::string &getArgName() const { return ArgName; } |
| 710 |
const std::string &getArgSizeName() const { return ArgSizeName; } |
| 711 |
bool isVariadic() const override { return true; } |
| 712 |
|
| 713 |
void writeAccessors(raw_ostream &OS) const override { |
| 714 |
std::string IteratorType = getLowerName().str() + "_iterator"; |
| 715 |
std::string BeginFn = getLowerName().str() + "_begin()"; |
| 716 |
std::string EndFn = getLowerName().str() + "_end()"; |
| 717 |
|
| 718 |
OS << " typedef " << Type << "* " << IteratorType << ";\n"; |
| 719 |
OS << " " << IteratorType << " " << BeginFn << " const {" |
| 720 |
<< " return " << ArgName << "; }\n"; |
| 721 |
OS << " " << IteratorType << " " << EndFn << " const {" |
| 722 |
<< " return " << ArgName << " + " << ArgSizeName << "; }\n"; |
| 723 |
OS << " unsigned " << getLowerName() << "_size() const {" |
| 724 |
<< " return " << ArgSizeName << "; }\n"; |
| 725 |
OS << " llvm::iterator_range<" << IteratorType << "> " << RangeName |
| 726 |
<< "() const { return llvm::make_range(" << BeginFn << ", " << EndFn |
| 727 |
<< "); }\n"; |
| 728 |
} |
| 729 |
|
| 730 |
void writeSetter(raw_ostream &OS) const { |
| 731 |
OS << " void set" << getUpperName() << "(ASTContext &Ctx, "; |
| 732 |
writeCtorParameters(OS); |
| 733 |
OS << ") {\n"; |
| 734 |
OS << " " << ArgSizeName << " = " << getUpperName() << "Size;\n"; |
| 735 |
OS << " " << ArgName << " = new (Ctx, 16) " << getType() << "[" |
| 736 |
<< ArgSizeName << "];\n"; |
| 737 |
OS << " "; |
| 738 |
writeCtorBody(OS); |
| 739 |
OS << " }\n"; |
| 740 |
} |
| 741 |
|
| 742 |
void writeCloneArgs(raw_ostream &OS) const override { |
| 743 |
OS << ArgName << ", " << ArgSizeName; |
| 744 |
} |
| 745 |
|
| 746 |
void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
| 747 |
// This isn't elegant, but we have to go through public methods... |
| 748 |
OS << "A->" << getLowerName() << "_begin(), " |
| 749 |
<< "A->" << getLowerName() << "_size()"; |
| 750 |
} |
| 751 |
|
| 752 |
void writeASTVisitorTraversal(raw_ostream &OS) const override { |
| 753 |
// FIXME: Traverse the elements. |
| 754 |
} |
| 755 |
|
| 756 |
void writeCtorBody(raw_ostream &OS) const override { |
| 757 |
OS << " std::copy(" << getUpperName() << ", " << getUpperName() << " + " |
| 758 |
<< ArgSizeName << ", " << ArgName << ");\n"; |
| 759 |
} |
| 760 |
|
| 761 |
void writeCtorInitializers(raw_ostream &OS) const override { |
| 762 |
OS << ArgSizeName << "(" << getUpperName() << "Size), " |
| 763 |
<< ArgName << "(new (Ctx, 16) " << getType() << "[" |
| 764 |
<< ArgSizeName << "])"; |
| 765 |
} |
| 766 |
|
| 767 |
void writeCtorDefaultInitializers(raw_ostream &OS) const override { |
| 768 |
OS << ArgSizeName << "(0), " << ArgName << "(nullptr)"; |
| 769 |
} |
| 770 |
|
| 771 |
void writeCtorParameters(raw_ostream &OS) const override { |
| 772 |
OS << getType() << " *" << getUpperName() << ", unsigned " |
| 773 |
<< getUpperName() << "Size"; |
| 774 |
} |
| 775 |
|
| 776 |
void writeImplicitCtorArgs(raw_ostream &OS) const override { |
| 777 |
OS << getUpperName() << ", " << getUpperName() << "Size"; |
| 778 |
} |
| 779 |
|
| 780 |
void writeDeclarations(raw_ostream &OS) const override { |
| 781 |
OS << " unsigned " << ArgSizeName << ";\n"; |
| 782 |
OS << " " << getType() << " *" << ArgName << ";"; |
| 783 |
} |
| 784 |
|
| 785 |
void writePCHReadDecls(raw_ostream &OS) const override { |
| 786 |
OS << " unsigned " << getLowerName() << "Size = Record.readInt();\n"; |
| 787 |
OS << " SmallVector<" << getType() << ", 4> " |
| 788 |
<< getLowerName() << ";\n"; |
| 789 |
OS << " " << getLowerName() << ".reserve(" << getLowerName() |
| 790 |
<< "Size);\n"; |
| 791 |
|
| 792 |
// If we can't store the values in the current type (if it's something |
| 793 |
// like StringRef), store them in a different type and convert the |
| 794 |
// container afterwards. |
| 795 |
std::string StorageType = getStorageType(getType()).str(); |
| 796 |
std::string StorageName = getLowerName().str(); |
| 797 |
if (StorageType != getType()) { |
| 798 |
StorageName += "Storage"; |
| 799 |
OS << " SmallVector<" << StorageType << ", 4> " |
| 800 |
<< StorageName << ";\n"; |
| 801 |
OS << " " << StorageName << ".reserve(" << getLowerName() |
| 802 |
<< "Size);\n"; |
| 803 |
} |
| 804 |
|
| 805 |
OS << " for (unsigned i = 0; i != " << getLowerName() << "Size; ++i)\n"; |
| 806 |
std::string read = ReadPCHRecord(Type); |
| 807 |
OS << " " << StorageName << ".push_back(" << read << ");\n"; |
| 808 |
|
| 809 |
if (StorageType != getType()) { |
| 810 |
OS << " for (unsigned i = 0; i != " << getLowerName() << "Size; ++i)\n"; |
| 811 |
OS << " " << getLowerName() << ".push_back(" |
| 812 |
<< StorageName << "[i]);\n"; |
| 813 |
} |
| 814 |
} |
| 815 |
|
| 816 |
void writePCHReadArgs(raw_ostream &OS) const override { |
| 817 |
OS << getLowerName() << ".data(), " << getLowerName() << "Size"; |
| 818 |
} |
| 819 |
|
| 820 |
void writePCHWrite(raw_ostream &OS) const override { |
| 821 |
OS << " Record.push_back(SA->" << getLowerName() << "_size());\n"; |
| 822 |
OS << " for (auto &Val : SA->" << RangeName << "())\n"; |
| 823 |
OS << " " << WritePCHRecord(Type, "Val"); |
| 824 |
} |
| 825 |
|
| 826 |
void writeValue(raw_ostream &OS) const override { |
| 827 |
OS << "\";\n"; |
| 828 |
OS << " for (const auto &Val : " << RangeName << "()) {\n" |
| 829 |
<< " DelimitAttributeArgument(OS, IsFirstArgument);\n"; |
| 830 |
writeValueImpl(OS); |
| 831 |
OS << " }\n"; |
| 832 |
OS << " OS << \""; |
| 833 |
} |
| 834 |
|
| 835 |
void writeDump(raw_ostream &OS) const override { |
| 836 |
OS << " for (const auto &Val : SA->" << RangeName << "())\n"; |
| 837 |
writeDumpImpl(OS); |
| 838 |
} |
| 839 |
}; |
| 840 |
|
| 841 |
class VariadicOMPInteropInfoArgument : public VariadicArgument { |
| 842 |
public: |
| 843 |
VariadicOMPInteropInfoArgument(const Record &Arg, StringRef Attr) |
| 844 |
: VariadicArgument(Arg, Attr, "OMPInteropInfo") {} |
| 845 |
|
| 846 |
void writeDump(raw_ostream &OS) const override { |
| 847 |
OS << " for (" << getAttrName() << "Attr::" << getLowerName() |
| 848 |
<< "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->" |
| 849 |
<< getLowerName() << "_end(); I != E; ++I) {\n"; |
| 850 |
OS << " if (I->IsTarget && I->IsTargetSync)\n"; |
| 851 |
OS << " OS << \" Target_TargetSync\";\n"; |
| 852 |
OS << " else if (I->IsTarget)\n"; |
| 853 |
OS << " OS << \" Target\";\n"; |
| 854 |
OS << " else\n"; |
| 855 |
OS << " OS << \" TargetSync\";\n"; |
| 856 |
OS << " }\n"; |
| 857 |
} |
| 858 |
|
| 859 |
void writePCHReadDecls(raw_ostream &OS) const override { |
| 860 |
OS << " unsigned " << getLowerName() << "Size = Record.readInt();\n"; |
| 861 |
OS << " SmallVector<OMPInteropInfo, 4> " << getLowerName() << ";\n"; |
| 862 |
OS << " " << getLowerName() << ".reserve(" << getLowerName() |
| 863 |
<< "Size);\n"; |
| 864 |
OS << " for (unsigned I = 0, E = " << getLowerName() << "Size; "; |
| 865 |
OS << "I != E; ++I) {\n"; |
| 866 |
OS << " bool IsTarget = Record.readBool();\n"; |
| 867 |
OS << " bool IsTargetSync = Record.readBool();\n"; |
| 868 |
OS << " " << getLowerName() |
| 869 |
<< ".emplace_back(IsTarget, IsTargetSync);\n"; |
| 870 |
OS << " }\n"; |
| 871 |
} |
| 872 |
|
| 873 |
void writePCHWrite(raw_ostream &OS) const override { |
| 874 |
OS << " Record.push_back(SA->" << getLowerName() << "_size());\n"; |
| 875 |
OS << " for (" << getAttrName() << "Attr::" << getLowerName() |
| 876 |
<< "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->" |
| 877 |
<< getLowerName() << "_end(); I != E; ++I) {\n"; |
| 878 |
OS << " Record.writeBool(I->IsTarget);\n"; |
| 879 |
OS << " Record.writeBool(I->IsTargetSync);\n"; |
| 880 |
OS << " }\n"; |
| 881 |
} |
| 882 |
}; |
| 883 |
|
| 884 |
class VariadicParamIdxArgument : public VariadicArgument { |
| 885 |
public: |
| 886 |
VariadicParamIdxArgument(const Record &Arg, StringRef Attr) |
| 887 |
: VariadicArgument(Arg, Attr, "ParamIdx") {} |
| 888 |
|
| 889 |
public: |
| 890 |
void writeValueImpl(raw_ostream &OS) const override { |
| 891 |
OS << " OS << Val.getSourceIndex();\n"; |
| 892 |
} |
| 893 |
|
| 894 |
void writeDumpImpl(raw_ostream &OS) const override { |
| 895 |
OS << " OS << \" \" << Val.getSourceIndex();\n"; |
| 896 |
} |
| 897 |
}; |
| 898 |
|
| 899 |
struct VariadicParamOrParamIdxArgument : public VariadicArgument { |
| 900 |
VariadicParamOrParamIdxArgument(const Record &Arg, StringRef Attr) |
| 901 |
: VariadicArgument(Arg, Attr, "int") {} |
| 902 |
}; |
| 903 |
|
| 904 |
// Unique the enums, but maintain the original declaration ordering. |
| 905 |
std::vector<StringRef> |
| 906 |
uniqueEnumsInOrder(const std::vector<StringRef> &enums) { |
| 907 |
std::vector<StringRef> uniques; |
| 908 |
SmallDenseSet<StringRef, 8> unique_set; |
| 909 |
for (const auto &i : enums) { |
| 910 |
if (unique_set.insert(i).second) |
| 911 |
uniques.push_back(i); |
| 912 |
} |
| 913 |
return uniques; |
| 914 |
} |
| 915 |
|
| 916 |
class EnumArgument : public Argument { |
| 917 |
std::string fullType; |
| 918 |
StringRef shortType; |
| 919 |
std::vector<StringRef> values, enums, uniques; |
| 920 |
bool isExternal; |
| 921 |
bool isCovered; |
| 922 |
|
| 923 |
public: |
| 924 |
EnumArgument(const Record &Arg, StringRef Attr) |
| 925 |
: Argument(Arg, Attr), values(Arg.getValueAsListOfStrings("Values")), |
| 926 |
enums(Arg.getValueAsListOfStrings("Enums")), |
| 927 |
uniques(uniqueEnumsInOrder(enums)), |
| 928 |
isExternal(Arg.getValueAsBit("IsExternalType")), |
| 929 |
isCovered(Arg.getValueAsBit("IsCovered")) { |
| 930 |
StringRef Type = Arg.getValueAsString("Type"); |
| 931 |
shortType = isExternal ? Type.rsplit("::").second : Type; |
| 932 |
// If shortType didn't contain :: at all rsplit will give us an empty |
| 933 |
// string. |
| 934 |
if (shortType.empty()) |
| 935 |
shortType = Type; |
| 936 |
fullType = isExternal ? Type : (getAttrName() + "Attr::" + Type).str(); |
| 937 |
|
| 938 |
// FIXME: Emit a proper error |
| 939 |
assert(!uniques.empty()); |
| 940 |
} |
| 941 |
|
| 942 |
bool isEnumArg() const override { return true; } |
| 943 |
|
| 944 |
void writeAccessors(raw_ostream &OS) const override { |
| 945 |
OS << " " << fullType << " get" << getUpperName() << "() const {\n"; |
| 946 |
OS << " return " << getLowerName() << ";\n"; |
| 947 |
OS << " }"; |
| 948 |
} |
| 949 |
|
| 950 |
void writeCloneArgs(raw_ostream &OS) const override { |
| 951 |
OS << getLowerName(); |
| 952 |
} |
| 953 |
|
| 954 |
void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
| 955 |
OS << "A->get" << getUpperName() << "()"; |
| 956 |
} |
| 957 |
void writeCtorInitializers(raw_ostream &OS) const override { |
| 958 |
OS << getLowerName() << "(" << getUpperName() << ")"; |
| 959 |
} |
| 960 |
void writeCtorDefaultInitializers(raw_ostream &OS) const override { |
| 961 |
OS << getLowerName() << "(" << fullType << "(0))"; |
| 962 |
} |
| 963 |
void writeCtorParameters(raw_ostream &OS) const override { |
| 964 |
OS << fullType << " " << getUpperName(); |
| 965 |
} |
| 966 |
void writeDeclarations(raw_ostream &OS) const override { |
| 967 |
if (!isExternal) { |
| 968 |
auto i = uniques.cbegin(), e = uniques.cend(); |
| 969 |
// The last one needs to not have a comma. |
| 970 |
--e; |
| 971 |
|
| 972 |
OS << "public:\n"; |
| 973 |
OS << " enum " << shortType << " {\n"; |
| 974 |
for (; i != e; ++i) |
| 975 |
OS << " " << *i << ",\n"; |
| 976 |
OS << " " << *e << "\n"; |
| 977 |
OS << " };\n"; |
| 978 |
} |
| 979 |
|
| 980 |
OS << "private:\n"; |
| 981 |
OS << " " << fullType << " " << getLowerName() << ";"; |
| 982 |
} |
| 983 |
|
| 984 |
void writePCHReadDecls(raw_ostream &OS) const override { |
| 985 |
OS << " " << fullType << " " << getLowerName() << "(static_cast<" |
| 986 |
<< fullType << ">(Record.readInt()));\n"; |
| 987 |
} |
| 988 |
|
| 989 |
void writePCHReadArgs(raw_ostream &OS) const override { |
| 990 |
OS << getLowerName(); |
| 991 |
} |
| 992 |
|
| 993 |
void writePCHWrite(raw_ostream &OS) const override { |
| 994 |
OS << "Record.push_back(static_cast<uint64_t>(SA->get" << getUpperName() |
| 995 |
<< "()));\n"; |
| 996 |
} |
| 997 |
|
| 998 |
void writeValue(raw_ostream &OS) const override { |
| 999 |
// FIXME: this isn't 100% correct -- some enum arguments require printing |
| 1000 |
// as a string literal, while others require printing as an identifier. |
| 1001 |
// Tablegen currently does not distinguish between the two forms. |
| 1002 |
OS << "\\\"\" << " << getAttrName() << "Attr::Convert" << shortType |
| 1003 |
<< "ToStr(get" << getUpperName() << "()) << \"\\\""; |
| 1004 |
} |
| 1005 |
|
| 1006 |
void writeDump(raw_ostream &OS) const override { |
| 1007 |
OS << " switch(SA->get" << getUpperName() << "()) {\n"; |
| 1008 |
for (const auto &I : uniques) { |
| 1009 |
OS << " case " << fullType << "::" << I << ":\n"; |
| 1010 |
OS << " OS << \" " << I << "\";\n"; |
| 1011 |
OS << " break;\n"; |
| 1012 |
} |
| 1013 |
if (!isCovered) { |
| 1014 |
OS << " default:\n"; |
| 1015 |
OS << " llvm_unreachable(\"Invalid attribute value\");\n"; |
| 1016 |
} |
| 1017 |
OS << " }\n"; |
| 1018 |
} |
| 1019 |
|
| 1020 |
void writeConversion(raw_ostream &OS, bool Header) const { |
| 1021 |
if (Header) { |
| 1022 |
OS << " static bool ConvertStrTo" << shortType << "(StringRef Val, " |
| 1023 |
<< fullType << " &Out);\n"; |
| 1024 |
OS << " static const char *Convert" << shortType << "ToStr(" |
| 1025 |
<< fullType << " Val);\n"; |
| 1026 |
return; |
| 1027 |
} |
| 1028 |
|
| 1029 |
OS << "bool " << getAttrName() << "Attr::ConvertStrTo" << shortType |
| 1030 |
<< "(StringRef Val, " << fullType << " &Out) {\n"; |
| 1031 |
OS << " std::optional<" << fullType << "> " |
| 1032 |
<< "R = llvm::StringSwitch<std::optional<" << fullType << ">>(Val)\n"; |
| 1033 |
for (size_t I = 0; I < enums.size(); ++I) { |
| 1034 |
OS << " .Case(\"" << values[I] << "\", "; |
| 1035 |
OS << fullType << "::" << enums[I] << ")\n"; |
| 1036 |
} |
| 1037 |
OS << " .Default(std::optional<" << fullType << ">());\n"; |
| 1038 |
OS << " if (R) {\n"; |
| 1039 |
OS << " Out = *R;\n return true;\n }\n"; |
| 1040 |
OS << " return false;\n"; |
| 1041 |
OS << "}\n\n"; |
| 1042 |
|
| 1043 |
// Mapping from enumeration values back to enumeration strings isn't |
| 1044 |
// trivial because some enumeration values have multiple named |
| 1045 |
// enumerators, such as type_visibility(internal) and |
| 1046 |
// type_visibility(hidden) both mapping to TypeVisibilityAttr::Hidden. |
| 1047 |
OS << "const char *" << getAttrName() << "Attr::Convert" << shortType |
| 1048 |
<< "ToStr(" << fullType << " Val) {\n" |
| 1049 |
<< " switch(Val) {\n"; |
| 1050 |
SmallDenseSet<StringRef, 8> Uniques; |
| 1051 |
for (size_t I = 0; I < enums.size(); ++I) { |
| 1052 |
if (Uniques.insert(enums[I]).second) |
| 1053 |
OS << " case " << fullType << "::" << enums[I] << ": return \"" |
| 1054 |
<< values[I] << "\";\n"; |
| 1055 |
} |
| 1056 |
if (!isCovered) { |
| 1057 |
OS << " default: llvm_unreachable(\"Invalid attribute value\");\n"; |
| 1058 |
} |
| 1059 |
OS << " }\n" |
| 1060 |
<< " llvm_unreachable(\"No enumerator with that value\");\n" |
| 1061 |
<< "}\n"; |
| 1062 |
} |
| 1063 |
}; |
| 1064 |
|
| 1065 |
class VariadicEnumArgument: public VariadicArgument { |
| 1066 |
std::string fullType; |
| 1067 |
StringRef shortType; |
| 1068 |
std::vector<StringRef> values, enums, uniques; |
| 1069 |
bool isExternal; |
| 1070 |
bool isCovered; |
| 1071 |
|
| 1072 |
protected: |
| 1073 |
void writeValueImpl(raw_ostream &OS) const override { |
| 1074 |
// FIXME: this isn't 100% correct -- some enum arguments require printing |
| 1075 |
// as a string literal, while others require printing as an identifier. |
| 1076 |
// Tablegen currently does not distinguish between the two forms. |
| 1077 |
OS << " OS << \"\\\"\" << " << getAttrName() << "Attr::Convert" |
| 1078 |
<< shortType << "ToStr(Val)" |
| 1079 |
<< "<< \"\\\"\";\n"; |
| 1080 |
} |
| 1081 |
|
| 1082 |
public: |
| 1083 |
VariadicEnumArgument(const Record &Arg, StringRef Attr) |
| 1084 |
: VariadicArgument(Arg, Attr, Arg.getValueAsString("Type").str()), |
| 1085 |
values(Arg.getValueAsListOfStrings("Values")), |
| 1086 |
enums(Arg.getValueAsListOfStrings("Enums")), |
| 1087 |
uniques(uniqueEnumsInOrder(enums)), |
| 1088 |
isExternal(Arg.getValueAsBit("IsExternalType")), |
| 1089 |
isCovered(Arg.getValueAsBit("IsCovered")) { |
| 1090 |
StringRef Type = Arg.getValueAsString("Type"); |
| 1091 |
shortType = isExternal ? Type.rsplit("::").second : Type; |
| 1092 |
// If shortType didn't contain :: at all rsplit will give us an empty |
| 1093 |
// string. |
| 1094 |
if (shortType.empty()) |
| 1095 |
shortType = Type; |
| 1096 |
fullType = isExternal ? Type : (getAttrName() + "Attr::" + Type).str(); |
| 1097 |
|
| 1098 |
// FIXME: Emit a proper error |
| 1099 |
assert(!uniques.empty()); |
| 1100 |
} |
| 1101 |
|
| 1102 |
bool isVariadicEnumArg() const override { return true; } |
| 1103 |
|
| 1104 |
void writeDeclarations(raw_ostream &OS) const override { |
| 1105 |
if (!isExternal) { |
| 1106 |
auto i = uniques.cbegin(), e = uniques.cend(); |
| 1107 |
// The last one needs to not have a comma. |
| 1108 |
--e; |
| 1109 |
|
| 1110 |
OS << "public:\n"; |
| 1111 |
OS << " enum " << shortType << " {\n"; |
| 1112 |
for (; i != e; ++i) |
| 1113 |
OS << " " << *i << ",\n"; |
| 1114 |
OS << " " << *e << "\n"; |
| 1115 |
OS << " };\n"; |
| 1116 |
} |
| 1117 |
OS << "private:\n"; |
| 1118 |
|
| 1119 |
VariadicArgument::writeDeclarations(OS); |
| 1120 |
} |
| 1121 |
|
| 1122 |
void writeDump(raw_ostream &OS) const override { |
| 1123 |
OS << " for (" << getAttrName() << "Attr::" << getLowerName() |
| 1124 |
<< "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->" |
| 1125 |
<< getLowerName() << "_end(); I != E; ++I) {\n"; |
| 1126 |
OS << " switch(*I) {\n"; |
| 1127 |
for (const auto &UI : uniques) { |
| 1128 |
OS << " case " << fullType << "::" << UI << ":\n"; |
| 1129 |
OS << " OS << \" " << UI << "\";\n"; |
| 1130 |
OS << " break;\n"; |
| 1131 |
} |
| 1132 |
if (!isCovered) { |
| 1133 |
OS << " default:\n"; |
| 1134 |
OS << " llvm_unreachable(\"Invalid attribute value\");\n"; |
| 1135 |
} |
| 1136 |
OS << " }\n"; |
| 1137 |
OS << " }\n"; |
| 1138 |
} |
| 1139 |
|
| 1140 |
void writePCHReadDecls(raw_ostream &OS) const override { |
| 1141 |
OS << " unsigned " << getLowerName() << "Size = Record.readInt();\n"; |
| 1142 |
OS << " SmallVector<" << fullType << ", 4> " << getLowerName() |
| 1143 |
<< ";\n"; |
| 1144 |
OS << " " << getLowerName() << ".reserve(" << getLowerName() |
| 1145 |
<< "Size);\n"; |
| 1146 |
OS << " for (unsigned i = " << getLowerName() << "Size; i; --i)\n"; |
| 1147 |
OS << " " << getLowerName() << ".push_back(" |
| 1148 |
<< "static_cast<" << fullType << ">(Record.readInt()));\n"; |
| 1149 |
} |
| 1150 |
|
| 1151 |
void writePCHWrite(raw_ostream &OS) const override { |
| 1152 |
OS << " Record.push_back(SA->" << getLowerName() << "_size());\n"; |
| 1153 |
OS << " for (" << getAttrName() << "Attr::" << getLowerName() |
| 1154 |
<< "_iterator i = SA->" << getLowerName() << "_begin(), e = SA->" |
| 1155 |
<< getLowerName() << "_end(); i != e; ++i)\n"; |
| 1156 |
OS << " " << WritePCHRecord(fullType, "(*i)"); |
| 1157 |
} |
| 1158 |
|
| 1159 |
void writeConversion(raw_ostream &OS, bool Header) const { |
| 1160 |
if (Header) { |
| 1161 |
OS << " static bool ConvertStrTo" << shortType << "(StringRef Val, " |
| 1162 |
<< fullType << " &Out);\n"; |
| 1163 |
OS << " static const char *Convert" << shortType << "ToStr(" |
| 1164 |
<< fullType << " Val);\n"; |
| 1165 |
return; |
| 1166 |
} |
| 1167 |
|
| 1168 |
OS << "bool " << getAttrName() << "Attr::ConvertStrTo" << shortType |
| 1169 |
<< "(StringRef Val, "; |
| 1170 |
OS << fullType << " &Out) {\n"; |
| 1171 |
OS << " std::optional<" << fullType |
| 1172 |
<< "> R = llvm::StringSwitch<std::optional<"; |
| 1173 |
OS << fullType << ">>(Val)\n"; |
| 1174 |
for (size_t I = 0; I < enums.size(); ++I) { |
| 1175 |
OS << " .Case(\"" << values[I] << "\", "; |
| 1176 |
OS << fullType << "::" << enums[I] << ")\n"; |
| 1177 |
} |
| 1178 |
OS << " .Default(std::optional<" << fullType << ">());\n"; |
| 1179 |
OS << " if (R) {\n"; |
| 1180 |
OS << " Out = *R;\n return true;\n }\n"; |
| 1181 |
OS << " return false;\n"; |
| 1182 |
OS << "}\n\n"; |
| 1183 |
|
| 1184 |
OS << "const char *" << getAttrName() << "Attr::Convert" << shortType |
| 1185 |
<< "ToStr(" << fullType << " Val) {\n" |
| 1186 |
<< " switch(Val) {\n"; |
| 1187 |
SmallDenseSet<StringRef, 8> Uniques; |
| 1188 |
for (size_t I = 0; I < enums.size(); ++I) { |
| 1189 |
if (Uniques.insert(enums[I]).second) |
| 1190 |
OS << " case " << fullType << "::" << enums[I] << ": return \"" |
| 1191 |
<< values[I] << "\";\n"; |
| 1192 |
} |
| 1193 |
if (!isCovered) { |
| 1194 |
OS << " default: llvm_unreachable(\"Invalid attribute value\");\n"; |
| 1195 |
} |
| 1196 |
OS << " }\n" |
| 1197 |
<< " llvm_unreachable(\"No enumerator with that value\");\n" |
| 1198 |
<< "}\n"; |
| 1199 |
} |
| 1200 |
}; |
| 1201 |
|
| 1202 |
class VersionArgument : public Argument { |
| 1203 |
public: |
| 1204 |
VersionArgument(const Record &Arg, StringRef Attr) |
| 1205 |
: Argument(Arg, Attr) |
| 1206 |
{} |
| 1207 |
|
| 1208 |
void writeAccessors(raw_ostream &OS) const override { |
| 1209 |
OS << " VersionTuple get" << getUpperName() << "() const {\n"; |
| 1210 |
OS << " return " << getLowerName() << ";\n"; |
| 1211 |
OS << " }\n"; |
| 1212 |
OS << " void set" << getUpperName() |
| 1213 |
<< "(ASTContext &C, VersionTuple V) {\n"; |
| 1214 |
OS << " " << getLowerName() << " = V;\n"; |
| 1215 |
OS << " }"; |
| 1216 |
} |
| 1217 |
|
| 1218 |
void writeCloneArgs(raw_ostream &OS) const override { |
| 1219 |
OS << "get" << getUpperName() << "()"; |
| 1220 |
} |
| 1221 |
|
| 1222 |
void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
| 1223 |
OS << "A->get" << getUpperName() << "()"; |
| 1224 |
} |
| 1225 |
|
| 1226 |
void writeCtorInitializers(raw_ostream &OS) const override { |
| 1227 |
OS << getLowerName() << "(" << getUpperName() << ")"; |
| 1228 |
} |
| 1229 |
|
| 1230 |
void writeCtorDefaultInitializers(raw_ostream &OS) const override { |
| 1231 |
OS << getLowerName() << "()"; |
| 1232 |
} |
| 1233 |
|
| 1234 |
void writeCtorParameters(raw_ostream &OS) const override { |
| 1235 |
OS << "VersionTuple " << getUpperName(); |
| 1236 |
} |
| 1237 |
|
| 1238 |
void writeDeclarations(raw_ostream &OS) const override { |
| 1239 |
OS << "VersionTuple " << getLowerName() << ";\n"; |
| 1240 |
} |
| 1241 |
|
| 1242 |
void writePCHReadDecls(raw_ostream &OS) const override { |
| 1243 |
OS << " VersionTuple " << getLowerName() |
| 1244 |
<< "= Record.readVersionTuple();\n"; |
| 1245 |
} |
| 1246 |
|
| 1247 |
void writePCHReadArgs(raw_ostream &OS) const override { |
| 1248 |
OS << getLowerName(); |
| 1249 |
} |
| 1250 |
|
| 1251 |
void writePCHWrite(raw_ostream &OS) const override { |
| 1252 |
OS << " Record.AddVersionTuple(SA->get" << getUpperName() << "());\n"; |
| 1253 |
} |
| 1254 |
|
| 1255 |
void writeValue(raw_ostream &OS) const override { |
| 1256 |
OS << getLowerName() << "=\" << get" << getUpperName() << "() << \""; |
| 1257 |
} |
| 1258 |
|
| 1259 |
void writeDump(raw_ostream &OS) const override { |
| 1260 |
OS << " OS << \" \" << SA->get" << getUpperName() << "();\n"; |
| 1261 |
} |
| 1262 |
}; |
| 1263 |
|
| 1264 |
class ExprArgument : public SimpleArgument { |
| 1265 |
public: |
| 1266 |
ExprArgument(const Record &Arg, StringRef Attr) |
| 1267 |
: SimpleArgument(Arg, Attr, "Expr *") |
| 1268 |
{} |
| 1269 |
|
| 1270 |
void writeASTVisitorTraversal(raw_ostream &OS) const override { |
| 1271 |
OS << " if (!" |
| 1272 |
<< "getDerived().TraverseStmt(A->get" << getUpperName() << "()))\n"; |
| 1273 |
OS << " return false;\n"; |
| 1274 |
} |
| 1275 |
|
| 1276 |
void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
| 1277 |
OS << "tempInst" << getUpperName(); |
| 1278 |
} |
| 1279 |
|
| 1280 |
void writeTemplateInstantiation(raw_ostream &OS) const override { |
| 1281 |
OS << " " << getType() << " tempInst" << getUpperName() << ";\n"; |
| 1282 |
OS << " {\n"; |
| 1283 |
OS << " EnterExpressionEvaluationContext " |
| 1284 |
<< "Unevaluated(S, Sema::ExpressionEvaluationContext::Unevaluated);\n"; |
| 1285 |
OS << " ExprResult " << "Result = S.SubstExpr(" |
| 1286 |
<< "A->get" << getUpperName() << "(), TemplateArgs);\n"; |
| 1287 |
OS << " if (Result.isInvalid())\n"; |
| 1288 |
OS << " return nullptr;\n"; |
| 1289 |
OS << " tempInst" << getUpperName() << " = Result.get();\n"; |
| 1290 |
OS << " }\n"; |
| 1291 |
} |
| 1292 |
|
| 1293 |
void writeValue(raw_ostream &OS) const override { |
| 1294 |
OS << "\";\n"; |
| 1295 |
OS << " get" << getUpperName() |
| 1296 |
<< "()->printPretty(OS, nullptr, Policy);\n"; |
| 1297 |
OS << " OS << \""; |
| 1298 |
} |
| 1299 |
|
| 1300 |
void writeDump(raw_ostream &OS) const override {} |
| 1301 |
|
| 1302 |
void writeDumpChildren(raw_ostream &OS) const override { |
| 1303 |
OS << " Visit(SA->get" << getUpperName() << "());\n"; |
| 1304 |
} |
| 1305 |
|
| 1306 |
void writeHasChildren(raw_ostream &OS) const override { OS << "true"; } |
| 1307 |
}; |
| 1308 |
|
| 1309 |
class VariadicExprArgument : public VariadicArgument { |
| 1310 |
public: |
| 1311 |
VariadicExprArgument(const Record &Arg, StringRef Attr) |
| 1312 |
: VariadicArgument(Arg, Attr, "Expr *") |
| 1313 |
{} |
| 1314 |
|
| 1315 |
VariadicExprArgument(StringRef ArgName, StringRef Attr) |
| 1316 |
: VariadicArgument(ArgName, Attr, "Expr *") {} |
| 1317 |
|
| 1318 |
void writeASTVisitorTraversal(raw_ostream &OS) const override { |
| 1319 |
OS << " {\n"; |
| 1320 |
OS << " " << getType() << " *I = A->" << getLowerName() |
| 1321 |
<< "_begin();\n"; |
| 1322 |
OS << " " << getType() << " *E = A->" << getLowerName() |
| 1323 |
<< "_end();\n"; |
| 1324 |
OS << " for (; I != E; ++I) {\n"; |
| 1325 |
OS << " if (!getDerived().TraverseStmt(*I))\n"; |
| 1326 |
OS << " return false;\n"; |
| 1327 |
OS << " }\n"; |
| 1328 |
OS << " }\n"; |
| 1329 |
} |
| 1330 |
|
| 1331 |
void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
| 1332 |
OS << "tempInst" << getUpperName() << ", " |
| 1333 |
<< "numTempInst" << getUpperName(); |
| 1334 |
} |
| 1335 |
|
| 1336 |
void writeTemplateInstantiation(raw_ostream &OS) const override { |
| 1337 |
OS << " size_t numTempInst" << getUpperName() << ";\n"; |
| 1338 |
OS << " " << getType() << "*tempInst" << getUpperName() << ";\n"; |
| 1339 |
OS << " {\n"; |
| 1340 |
OS << " EnterExpressionEvaluationContext " |
| 1341 |
<< "Unevaluated(S, Sema::ExpressionEvaluationContext::Unevaluated);\n"; |
| 1342 |
OS << " ArrayRef<" << getType() << "> ArgsToInstantiate(A->" |
| 1343 |
<< getLowerName() << "_begin(), A->" << getLowerName() << "_end());\n"; |
| 1344 |
OS << " SmallVector<" << getType() << ", 4> InstArgs;\n"; |
| 1345 |
OS << " if (S.SubstExprs(ArgsToInstantiate, /*IsCall=*/false, " |
| 1346 |
"TemplateArgs, InstArgs))\n"; |
| 1347 |
OS << " return nullptr;\n"; |
| 1348 |
OS << " numTempInst" << getUpperName() << " = InstArgs.size();\n"; |
| 1349 |
OS << " tempInst" << getUpperName() << " = new (C, 16) " |
| 1350 |
<< getType() << "[numTempInst" << getUpperName() << "];\n"; |
| 1351 |
OS << " std::copy(InstArgs.begin(), InstArgs.end(), tempInst" |
| 1352 |
<< getUpperName() << ");\n"; |
| 1353 |
OS << " }\n"; |
| 1354 |
} |
| 1355 |
|
| 1356 |
void writeDump(raw_ostream &OS) const override {} |
| 1357 |
|
| 1358 |
void writeDumpChildren(raw_ostream &OS) const override { |
| 1359 |
OS << " for (" << getAttrName() << "Attr::" << getLowerName() |
| 1360 |
<< "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->" |
| 1361 |
<< getLowerName() << "_end(); I != E; ++I)\n"; |
| 1362 |
OS << " Visit(*I);\n"; |
| 1363 |
} |
| 1364 |
|
| 1365 |
void writeHasChildren(raw_ostream &OS) const override { |
| 1366 |
OS << "SA->" << getLowerName() << "_begin() != " |
| 1367 |
<< "SA->" << getLowerName() << "_end()"; |
| 1368 |
} |
| 1369 |
}; |
| 1370 |
|
| 1371 |
class VariadicIdentifierArgument : public VariadicArgument { |
| 1372 |
public: |
| 1373 |
VariadicIdentifierArgument(const Record &Arg, StringRef Attr) |
| 1374 |
: VariadicArgument(Arg, Attr, "const IdentifierInfo *") {} |
| 1375 |
}; |
| 1376 |
|
| 1377 |
class VariadicStringArgument : public VariadicArgument { |
| 1378 |
public: |
| 1379 |
VariadicStringArgument(const Record &Arg, StringRef Attr) |
| 1380 |
: VariadicArgument(Arg, Attr, "StringRef") |
| 1381 |
{} |
| 1382 |
|
| 1383 |
void writeCtorBody(raw_ostream &OS) const override { |
| 1384 |
OS << " for (size_t I = 0, E = " << getArgSizeName() << "; I != E;\n" |
| 1385 |
" ++I) {\n" |
| 1386 |
" StringRef Ref = " << getUpperName() << "[I];\n" |
| 1387 |
" if (!Ref.empty()) {\n" |
| 1388 |
" char *Mem = new (Ctx, 1) char[Ref.size()];\n" |
| 1389 |
" std::memcpy(Mem, Ref.data(), Ref.size());\n" |
| 1390 |
" " << getArgName() << "[I] = StringRef(Mem, Ref.size());\n" |
| 1391 |
" }\n" |
| 1392 |
" }\n"; |
| 1393 |
} |
| 1394 |
|
| 1395 |
void writeValueImpl(raw_ostream &OS) const override { |
| 1396 |
OS << " OS << \"\\\"\" << Val << \"\\\"\";\n"; |
| 1397 |
} |
| 1398 |
}; |
| 1399 |
|
| 1400 |
class TypeArgument : public SimpleArgument { |
| 1401 |
public: |
| 1402 |
TypeArgument(const Record &Arg, StringRef Attr) |
| 1403 |
: SimpleArgument(Arg, Attr, "TypeSourceInfo *") |
| 1404 |
{} |
| 1405 |
|
| 1406 |
void writeAccessors(raw_ostream &OS) const override { |
| 1407 |
OS << " QualType get" << getUpperName() << "() const {\n"; |
| 1408 |
OS << " return " << getLowerName() << "->getType();\n"; |
| 1409 |
OS << " }"; |
| 1410 |
OS << " " << getType() << " get" << getUpperName() << "Loc() const {\n"; |
| 1411 |
OS << " return " << getLowerName() << ";\n"; |
| 1412 |
OS << " }"; |
| 1413 |
} |
| 1414 |
|
| 1415 |
void writeASTVisitorTraversal(raw_ostream &OS) const override { |
| 1416 |
OS << " if (auto *TSI = A->get" << getUpperName() << "Loc())\n"; |
| 1417 |
OS << " if (!getDerived().TraverseTypeLoc(TSI->getTypeLoc()))\n"; |
| 1418 |
OS << " return false;\n"; |
| 1419 |
} |
| 1420 |
|
| 1421 |
void writeTemplateInstantiation(raw_ostream &OS) const override { |
| 1422 |
OS << " " << getType() << " tempInst" << getUpperName() << " =\n"; |
| 1423 |
OS << " S.SubstType(A->get" << getUpperName() << "Loc(), " |
| 1424 |
<< "TemplateArgs, A->getLoc(), A->getAttrName());\n"; |
| 1425 |
OS << " if (!tempInst" << getUpperName() << ")\n"; |
| 1426 |
OS << " return nullptr;\n"; |
| 1427 |
} |
| 1428 |
|
| 1429 |
void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
| 1430 |
OS << "tempInst" << getUpperName(); |
| 1431 |
} |
| 1432 |
|
| 1433 |
void writePCHWrite(raw_ostream &OS) const override { |
| 1434 |
OS << " " |
| 1435 |
<< WritePCHRecord(getType(), |
| 1436 |
"SA->get" + getUpperName().str() + "Loc()"); |
| 1437 |
} |
| 1438 |
}; |
| 1439 |
|
| 1440 |
class WrappedAttr : public SimpleArgument { |
| 1441 |
public: |
| 1442 |
WrappedAttr(const Record &Arg, StringRef Attr) |
| 1443 |
: SimpleArgument(Arg, Attr, "Attr *") {} |
| 1444 |
|
| 1445 |
void writePCHReadDecls(raw_ostream &OS) const override { |
| 1446 |
OS << " Attr *" << getLowerName() << " = Record.readAttr();"; |
| 1447 |
} |
| 1448 |
|
| 1449 |
void writePCHWrite(raw_ostream &OS) const override { |
| 1450 |
OS << " AddAttr(SA->get" << getUpperName() << "());"; |
| 1451 |
} |
| 1452 |
|
| 1453 |
void writeDump(raw_ostream &OS) const override {} |
| 1454 |
|
| 1455 |
void writeDumpChildren(raw_ostream &OS) const override { |
| 1456 |
OS << " Visit(SA->get" << getUpperName() << "());\n"; |
| 1457 |
} |
| 1458 |
|
| 1459 |
void writeHasChildren(raw_ostream &OS) const override { OS << "true"; } |
| 1460 |
}; |
| 1461 |
|
| 1462 |
} // end anonymous namespace |
| 1463 |
|
| 1464 |
static std::unique_ptr<Argument> |
| 1465 |
createArgument(const Record &Arg, StringRef Attr, |
| 1466 |
const Record *Search = nullptr) { |
| 1467 |
if (!Search) |
| 1468 |
Search = &Arg; |
| 1469 |
|
| 1470 |
std::unique_ptr<Argument> Ptr; |
| 1471 |
StringRef ArgName = Search->getName(); |
| 1472 |
|
| 1473 |
if (ArgName == "AlignedArgument") |
| 1474 |
Ptr = std::make_unique<AlignedArgument>(Arg, Attr); |
| 1475 |
else if (ArgName == "EnumArgument") |
| 1476 |
Ptr = std::make_unique<EnumArgument>(Arg, Attr); |
| 1477 |
else if (ArgName == "ExprArgument") |
| 1478 |
Ptr = std::make_unique<ExprArgument>(Arg, Attr); |
| 1479 |
else if (ArgName == "DeclArgument") |
| 1480 |
Ptr = std::make_unique<SimpleArgument>( |
| 1481 |
Arg, Attr, (Arg.getValueAsDef("Kind")->getName() + "Decl *").str()); |
| 1482 |
else if (ArgName == "IdentifierArgument") |
| 1483 |
Ptr = std::make_unique<SimpleArgument>(Arg, Attr, "const IdentifierInfo *"); |
| 1484 |
else if (ArgName == "DefaultBoolArgument") |
| 1485 |
Ptr = std::make_unique<DefaultSimpleArgument>( |
| 1486 |
Arg, Attr, "bool", Arg.getValueAsBit("Default")); |
| 1487 |
else if (ArgName == "BoolArgument") |
| 1488 |
Ptr = std::make_unique<SimpleArgument>(Arg, Attr, "bool"); |
| 1489 |
else if (ArgName == "DefaultIntArgument") |
| 1490 |
Ptr = std::make_unique<DefaultSimpleArgument>( |
| 1491 |
Arg, Attr, "int", Arg.getValueAsInt("Default")); |
| 1492 |
else if (ArgName == "IntArgument") |
| 1493 |
Ptr = std::make_unique<SimpleArgument>(Arg, Attr, "int"); |
| 1494 |
else if (ArgName == "StringArgument") |
| 1495 |
Ptr = std::make_unique<StringArgument>(Arg, Attr); |
| 1496 |
else if (ArgName == "TypeArgument") |
| 1497 |
Ptr = std::make_unique<TypeArgument>(Arg, Attr); |
| 1498 |
else if (ArgName == "UnsignedArgument") |
| 1499 |
Ptr = std::make_unique<SimpleArgument>(Arg, Attr, "unsigned"); |
| 1500 |
else if (ArgName == "VariadicUnsignedArgument") |
| 1501 |
Ptr = std::make_unique<VariadicArgument>(Arg, Attr, "unsigned"); |
| 1502 |
else if (ArgName == "VariadicStringArgument") |
| 1503 |
Ptr = std::make_unique<VariadicStringArgument>(Arg, Attr); |
| 1504 |
else if (ArgName == "VariadicEnumArgument") |
| 1505 |
Ptr = std::make_unique<VariadicEnumArgument>(Arg, Attr); |
| 1506 |
else if (ArgName == "VariadicExprArgument") |
| 1507 |
Ptr = std::make_unique<VariadicExprArgument>(Arg, Attr); |
| 1508 |
else if (ArgName == "VariadicParamIdxArgument") |
| 1509 |
Ptr = std::make_unique<VariadicParamIdxArgument>(Arg, Attr); |
| 1510 |
else if (ArgName == "VariadicParamOrParamIdxArgument") |
| 1511 |
Ptr = std::make_unique<VariadicParamOrParamIdxArgument>(Arg, Attr); |
| 1512 |
else if (ArgName == "ParamIdxArgument") |
| 1513 |
Ptr = std::make_unique<SimpleArgument>(Arg, Attr, "ParamIdx"); |
| 1514 |
else if (ArgName == "VariadicIdentifierArgument") |
| 1515 |
Ptr = std::make_unique<VariadicIdentifierArgument>(Arg, Attr); |
| 1516 |
else if (ArgName == "VersionArgument") |
| 1517 |
Ptr = std::make_unique<VersionArgument>(Arg, Attr); |
| 1518 |
else if (ArgName == "WrappedAttr") |
| 1519 |
Ptr = std::make_unique<WrappedAttr>(Arg, Attr); |
| 1520 |
else if (ArgName == "OMPTraitInfoArgument") |
| 1521 |
Ptr = std::make_unique<SimpleArgument>(Arg, Attr, "OMPTraitInfo *"); |
| 1522 |
else if (ArgName == "VariadicOMPInteropInfoArgument") |
| 1523 |
Ptr = std::make_unique<VariadicOMPInteropInfoArgument>(Arg, Attr); |
| 1524 |
|
| 1525 |
if (!Ptr) { |
| 1526 |
// Search in reverse order so that the most-derived type is handled first. |
| 1527 |
std::vector<const Record *> SCs = Search->getSuperClasses(); |
| 1528 |
for (const Record *Base : reverse(SCs)) { |
| 1529 |
if ((Ptr = createArgument(Arg, Attr, Base))) |
| 1530 |
break; |
| 1531 |
} |
| 1532 |
} |
| 1533 |
|
| 1534 |
if (Ptr && Arg.getValueAsBit("Optional")) |
| 1535 |
Ptr->setOptional(true); |
| 1536 |
|
| 1537 |
if (Ptr && Arg.getValueAsBit("Fake")) |
| 1538 |
Ptr->setFake(true); |
| 1539 |
|
| 1540 |
return Ptr; |
| 1541 |
} |
| 1542 |
|
| 1543 |
static void writeAvailabilityValue(raw_ostream &OS) { |
| 1544 |
OS << "\" << getPlatform()->getName();\n" |
| 1545 |
<< " if (getStrict()) OS << \", strict\";\n" |
| 1546 |
<< " if (!getIntroduced().empty()) OS << \", introduced=\" << getIntroduced();\n" |
| 1547 |
<< " if (!getDeprecated().empty()) OS << \", deprecated=\" << getDeprecated();\n" |
| 1548 |
<< " if (!getObsoleted().empty()) OS << \", obsoleted=\" << getObsoleted();\n" |
| 1549 |
<< " if (getUnavailable()) OS << \", unavailable\";\n" |
| 1550 |
<< " OS << \""; |
| 1551 |
} |
| 1552 |
|
| 1553 |
static void writeDeprecatedAttrValue(raw_ostream &OS, StringRef Variety) { |
| 1554 |
OS << "\\\"\" << getMessage() << \"\\\"\";\n"; |
| 1555 |
// Only GNU deprecated has an optional fixit argument at the second position. |
| 1556 |
if (Variety == "GNU") |
| 1557 |
OS << " if (!getReplacement().empty()) OS << \", \\\"\"" |
| 1558 |
" << getReplacement() << \"\\\"\";\n"; |
| 1559 |
OS << " OS << \""; |
| 1560 |
} |
| 1561 |
|
| 1562 |
static void writeGetSpellingFunction(const Record &R, raw_ostream &OS) { |
| 1563 |
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R); |
| 1564 |
|
| 1565 |
OS << "const char *" << R.getName() << "Attr::getSpelling() const {\n"; |
| 1566 |
if (Spellings.empty()) { |
| 1567 |
OS << " return \"(No spelling)\";\n}\n\n"; |
| 1568 |
return; |
| 1569 |
} |
| 1570 |
|
| 1571 |
OS << " switch (getAttributeSpellingListIndex()) {\n" |
| 1572 |
" default:\n" |
| 1573 |
" llvm_unreachable(\"Unknown attribute spelling!\");\n" |
| 1574 |
" return \"(No spelling)\";\n"; |
| 1575 |
|
| 1576 |
for (const auto &[Idx, S] : enumerate(Spellings)) { |
| 1577 |
// clang-format off |
| 1578 |
OS << " case " << Idx << ":\n" |
| 1579 |
" return \"" << S.name() << "\";\n"; |
| 1580 |
// clang-format on |
| 1581 |
} |
| 1582 |
// End of the switch statement. |
| 1583 |
OS << " }\n"; |
| 1584 |
// End of the getSpelling function. |
| 1585 |
OS << "}\n\n"; |
| 1586 |
} |
| 1587 |
|
| 1588 |
static void |
| 1589 |
writePrettyPrintFunction(const Record &R, |
| 1590 |
const std::vector<std::unique_ptr<Argument>> &Args, |
| 1591 |
raw_ostream &OS) { |
| 1592 |
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R); |
| 1593 |
|
| 1594 |
OS << "void " << R.getName() << "Attr::printPretty(" |
| 1595 |
<< "raw_ostream &OS, const PrintingPolicy &Policy) const {\n"; |
| 1596 |
|
| 1597 |
if (Spellings.empty()) { |
| 1598 |
OS << "}\n\n"; |
| 1599 |
return; |
| 1600 |
} |
| 1601 |
|
| 1602 |
OS << " bool IsFirstArgument = true; (void)IsFirstArgument;\n" |
| 1603 |
<< " unsigned TrailingOmittedArgs = 0; (void)TrailingOmittedArgs;\n" |
| 1604 |
<< " switch (getAttributeSpellingListIndex()) {\n" |
| 1605 |
<< " default:\n" |
| 1606 |
<< " llvm_unreachable(\"Unknown attribute spelling!\");\n" |
| 1607 |
<< " break;\n"; |
| 1608 |
|
| 1609 |
for (const auto &[Idx, S] : enumerate(Spellings)) { |
| 1610 |
SmallString<16> Prefix; |
| 1611 |
SmallString<8> Suffix; |
| 1612 |
// The actual spelling of the name and namespace (if applicable) |
| 1613 |
// of an attribute without considering prefix and suffix. |
| 1614 |
SmallString<64> Spelling; |
| 1615 |
StringRef Name = S.name(); |
| 1616 |
StringRef Variety = S.variety(); |
| 1617 |
|
| 1618 |
if (Variety == "GNU") { |
| 1619 |
Prefix = "__attribute__(("; |
| 1620 |
Suffix = "))"; |
| 1621 |
} else if (Variety == "CXX11" || Variety == "C23") { |
| 1622 |
Prefix = "[["; |
| 1623 |
Suffix = "]]"; |
| 1624 |
StringRef Namespace = S.nameSpace(); |
| 1625 |
if (!Namespace.empty()) { |
| 1626 |
Spelling += Namespace; |
| 1627 |
Spelling += "::"; |
| 1628 |
} |
| 1629 |
} else if (Variety == "Declspec") { |
| 1630 |
Prefix = "__declspec("; |
| 1631 |
Suffix = ")"; |
| 1632 |
} else if (Variety == "Microsoft") { |
| 1633 |
Prefix = "["; |
| 1634 |
Suffix = "]"; |
| 1635 |
} else if (Variety == "Keyword") { |
| 1636 |
Prefix = ""; |
| 1637 |
Suffix = ""; |
| 1638 |
} else if (Variety == "Pragma") { |
| 1639 |
Prefix = "#pragma "; |
| 1640 |
Suffix = "\n"; |
| 1641 |
StringRef Namespace = S.nameSpace(); |
| 1642 |
if (!Namespace.empty()) { |
| 1643 |
Spelling += Namespace; |
| 1644 |
Spelling += " "; |
| 1645 |
} |
| 1646 |
} else if (Variety == "HLSLAnnotation") { |
| 1647 |
Prefix = ":"; |
| 1648 |
Suffix = ""; |
| 1649 |
} else { |
| 1650 |
llvm_unreachable("Unknown attribute syntax variety!"); |
| 1651 |
} |
| 1652 |
|
| 1653 |
Spelling += Name; |
| 1654 |
|
| 1655 |
OS << " case " << Idx << " : {\n" |
| 1656 |
<< " OS << \"" << Prefix << Spelling << "\";\n"; |
| 1657 |
|
| 1658 |
if (Variety == "Pragma") { |
| 1659 |
OS << " printPrettyPragma(OS, Policy);\n"; |
| 1660 |
OS << " OS << \"\\n\";"; |
| 1661 |
OS << " break;\n"; |
| 1662 |
OS << " }\n"; |
| 1663 |
continue; |
| 1664 |
} |
| 1665 |
|
| 1666 |
if (Spelling == "availability") { |
| 1667 |
OS << " OS << \"("; |
| 1668 |
writeAvailabilityValue(OS); |
| 1669 |
OS << ")\";\n"; |
| 1670 |
} else if (Spelling == "deprecated" || Spelling == "gnu::deprecated") { |
| 1671 |
OS << " OS << \"("; |
| 1672 |
writeDeprecatedAttrValue(OS, Variety); |
| 1673 |
OS << ")\";\n"; |
| 1674 |
} else { |
| 1675 |
// To avoid printing parentheses around an empty argument list or |
| 1676 |
// printing spurious commas at the end of an argument list, we need to |
| 1677 |
// determine where the last provided non-fake argument is. |
| 1678 |
bool FoundNonOptArg = false; |
| 1679 |
for (const auto &arg : reverse(Args)) { |
| 1680 |
if (arg->isFake()) |
| 1681 |
continue; |
| 1682 |
if (FoundNonOptArg) |
| 1683 |
continue; |
| 1684 |
// FIXME: arg->getIsOmitted() == "false" means we haven't implemented |
| 1685 |
// any way to detect whether the argument was omitted. |
| 1686 |
if (!arg->isOptional() || arg->getIsOmitted() == "false") { |
| 1687 |
FoundNonOptArg = true; |
| 1688 |
continue; |
| 1689 |
} |
| 1690 |
OS << " if (" << arg->getIsOmitted() << ")\n" |
| 1691 |
<< " ++TrailingOmittedArgs;\n"; |
| 1692 |
} |
| 1693 |
unsigned ArgIndex = 0; |
| 1694 |
for (const auto &arg : Args) { |
| 1695 |
if (arg->isFake()) |
| 1696 |
continue; |
| 1697 |
std::string IsOmitted = arg->getIsOmitted(); |
| 1698 |
if (arg->isOptional() && IsOmitted != "false") |
| 1699 |
OS << " if (!(" << IsOmitted << ")) {\n"; |
| 1700 |
// Variadic arguments print their own leading comma. |
| 1701 |
if (!arg->isVariadic()) |
| 1702 |
OS << " DelimitAttributeArgument(OS, IsFirstArgument);\n"; |
| 1703 |
OS << " OS << \""; |
| 1704 |
arg->writeValue(OS); |
| 1705 |
OS << "\";\n"; |
| 1706 |
if (arg->isOptional() && IsOmitted != "false") |
| 1707 |
OS << " }\n"; |
| 1708 |
++ArgIndex; |
| 1709 |
} |
| 1710 |
if (ArgIndex != 0) |
| 1711 |
OS << " if (!IsFirstArgument)\n" |
| 1712 |
<< " OS << \")\";\n"; |
| 1713 |
} |
| 1714 |
OS << " OS << \"" << Suffix << "\";\n" |
| 1715 |
<< " break;\n" |
| 1716 |
<< " }\n"; |
| 1717 |
} |
| 1718 |
|
| 1719 |
// End of the switch statement. |
| 1720 |
OS << "}\n"; |
| 1721 |
// End of the print function. |
| 1722 |
OS << "}\n\n"; |
| 1723 |
} |
| 1724 |
|
| 1725 |
/// Return the index of a spelling in a spelling list. |
| 1726 |
static unsigned getSpellingListIndex(ArrayRef<FlattenedSpelling> SpellingList, |
| 1727 |
const FlattenedSpelling &Spelling) { |
| 1728 |
assert(!SpellingList.empty() && "Spelling list is empty!"); |
| 1729 |
|
| 1730 |
for (const auto &[Index, S] : enumerate(SpellingList)) { |
| 1731 |
if (S.variety() == Spelling.variety() && |
| 1732 |
S.nameSpace() == Spelling.nameSpace() && S.name() == Spelling.name()) |
| 1733 |
return Index; |
| 1734 |
} |
| 1735 |
|
| 1736 |
PrintFatalError("Unknown spelling: " + Spelling.name()); |
| 1737 |
} |
| 1738 |
|
| 1739 |
static void writeAttrAccessorDefinition(const Record &R, raw_ostream &OS) { |
| 1740 |
std::vector<const Record *> Accessors = R.getValueAsListOfDefs("Accessors"); |
| 1741 |
if (Accessors.empty()) |
| 1742 |
return; |
| 1743 |
|
| 1744 |
const std::vector<FlattenedSpelling> SpellingList = GetFlattenedSpellings(R); |
| 1745 |
assert(!SpellingList.empty() && |
| 1746 |
"Attribute with empty spelling list can't have accessors!"); |
| 1747 |
for (const auto *Accessor : Accessors) { |
| 1748 |
const StringRef Name = Accessor->getValueAsString("Name"); |
| 1749 |
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Accessor); |
| 1750 |
|
| 1751 |
OS << " bool " << Name |
| 1752 |
<< "() const { return getAttributeSpellingListIndex() == "; |
| 1753 |
for (unsigned Index = 0; Index < Spellings.size(); ++Index) { |
| 1754 |
OS << getSpellingListIndex(SpellingList, Spellings[Index]); |
| 1755 |
if (Index != Spellings.size() - 1) |
| 1756 |
OS << " ||\n getAttributeSpellingListIndex() == "; |
| 1757 |
else |
| 1758 |
OS << "; }\n"; |
| 1759 |
} |
| 1760 |
} |
| 1761 |
} |
| 1762 |
|
| 1763 |
static bool |
| 1764 |
SpellingNamesAreCommon(const std::vector<FlattenedSpelling>& Spellings) { |
| 1765 |
assert(!Spellings.empty() && "An empty list of spellings was provided"); |
| 1766 |
StringRef FirstName = |
| 1767 |
NormalizeNameForSpellingComparison(Spellings.front().name()); |
| 1768 |
for (const auto &Spelling : drop_begin(Spellings)) { |
| 1769 |
StringRef Name = NormalizeNameForSpellingComparison(Spelling.name()); |
| 1770 |
if (Name != FirstName) |
| 1771 |
return false; |
| 1772 |
} |
| 1773 |
return true; |
| 1774 |
} |
| 1775 |
|
| 1776 |
typedef std::map<unsigned, std::string> SemanticSpellingMap; |
| 1777 |
static std::string |
| 1778 |
CreateSemanticSpellings(const std::vector<FlattenedSpelling> &Spellings, |
| 1779 |
SemanticSpellingMap &Map) { |
| 1780 |
// The enumerants are automatically generated based on the variety, |
| 1781 |
// namespace (if present) and name for each attribute spelling. However, |
| 1782 |
// care is taken to avoid trampling on the reserved namespace due to |
| 1783 |
// underscores. |
| 1784 |
std::string Ret(" enum Spelling {\n"); |
| 1785 |
std::set<std::string> Uniques; |
| 1786 |
unsigned Idx = 0; |
| 1787 |
|
| 1788 |
// If we have a need to have this many spellings we likely need to add an |
| 1789 |
// extra bit to the SpellingIndex in AttributeCommonInfo, then increase the |
| 1790 |
// value of SpellingNotCalculated there and here. |
| 1791 |
assert(Spellings.size() < 15 && |
| 1792 |
"Too many spellings, would step on SpellingNotCalculated in " |
| 1793 |
"AttributeCommonInfo"); |
| 1794 |
for (auto I = Spellings.begin(), E = Spellings.end(); I != E; ++I, ++Idx) { |
| 1795 |
const FlattenedSpelling &S = *I; |
| 1796 |
StringRef Variety = S.variety(); |
| 1797 |
StringRef Spelling = S.name(); |
| 1798 |
StringRef Namespace = S.nameSpace(); |
| 1799 |
std::string EnumName; |
| 1800 |
|
| 1801 |
EnumName += Variety; |
| 1802 |
EnumName += "_"; |
| 1803 |
if (!Namespace.empty()) |
| 1804 |
EnumName += NormalizeNameForSpellingComparison(Namespace).str() + "_"; |
| 1805 |
EnumName += NormalizeNameForSpellingComparison(Spelling); |
| 1806 |
|
| 1807 |
// Even if the name is not unique, this spelling index corresponds to a |
| 1808 |
// particular enumerant name that we've calculated. |
| 1809 |
Map[Idx] = EnumName; |
| 1810 |
|
| 1811 |
// Since we have been stripping underscores to avoid trampling on the |
| 1812 |
// reserved namespace, we may have inadvertently created duplicate |
| 1813 |
// enumerant names. These duplicates are not considered part of the |
| 1814 |
// semantic spelling, and can be elided. |
| 1815 |
if (!Uniques.insert(EnumName).second) |
| 1816 |
continue; |
| 1817 |
|
| 1818 |
if (I != Spellings.begin()) |
| 1819 |
Ret += ",\n"; |
| 1820 |
// Duplicate spellings are not considered part of the semantic spelling |
| 1821 |
// enumeration, but the spelling index and semantic spelling values are |
| 1822 |
// meant to be equivalent, so we must specify a concrete value for each |
| 1823 |
// enumerator. |
| 1824 |
Ret += " " + EnumName + " = " + utostr(Idx); |
| 1825 |
} |
| 1826 |
Ret += ",\n SpellingNotCalculated = 15\n"; |
| 1827 |
Ret += "\n };\n\n"; |
| 1828 |
return Ret; |
| 1829 |
} |
| 1830 |
|
| 1831 |
static void WriteSemanticSpellingSwitch(StringRef VarName, |
| 1832 |
const SemanticSpellingMap &Map, |
| 1833 |
raw_ostream &OS) { |
| 1834 |
OS << " switch (" << VarName << ") {\n default: " |
| 1835 |
<< "llvm_unreachable(\"Unknown spelling list index\");\n"; |
| 1836 |
for (const auto &I : Map) |
| 1837 |
OS << " case " << I.first << ": return " << I.second << ";\n"; |
| 1838 |
OS << " }\n"; |
| 1839 |
} |
| 1840 |
|
| 1841 |
// Note: these values need to match the values used by LateAttrParseKind in |
| 1842 |
// `Attr.td` |
| 1843 |
enum class LateAttrParseKind { Never = 0, Standard = 1, ExperimentalExt = 2 }; |
| 1844 |
|
| 1845 |
static LateAttrParseKind getLateAttrParseKind(const Record *Attr) { |
| 1846 |
// This function basically does |
| 1847 |
// `Attr->getValueAsDef("LateParsed")->getValueAsInt("Kind")` but does a bunch |
| 1848 |
// of sanity checking to ensure that `LateAttrParseMode` in `Attr.td` is in |
| 1849 |
// sync with the `LateAttrParseKind` enum in this source file. |
| 1850 |
|
| 1851 |
static constexpr StringRef LateParsedStr = "LateParsed"; |
| 1852 |
static constexpr StringRef LateAttrParseKindStr = "LateAttrParseKind"; |
| 1853 |
static constexpr StringRef KindFieldStr = "Kind"; |
| 1854 |
|
| 1855 |
auto *LAPK = Attr->getValueAsDef(LateParsedStr); |
| 1856 |
|
| 1857 |
// Typecheck the `LateParsed` field. |
| 1858 |
if (LAPK->getDirectSuperClasses().size() != 1) |
| 1859 |
PrintFatalError(Attr, "Field `" + Twine(LateParsedStr) + |
| 1860 |
"`should only have one super class"); |
| 1861 |
|
| 1862 |
const Record *SuperClass = LAPK->getDirectSuperClasses()[0].first; |
| 1863 |
if (SuperClass->getName() != LateAttrParseKindStr) |
| 1864 |
PrintFatalError( |
| 1865 |
Attr, "Field `" + Twine(LateParsedStr) + "`should only have type `" + |
| 1866 |
Twine(LateAttrParseKindStr) + "` but found type `" + |
| 1867 |
SuperClass->getName() + "`"); |
| 1868 |
|
| 1869 |
// Get Kind and verify the enum name matches the name in `Attr.td`. |
| 1870 |
unsigned Kind = LAPK->getValueAsInt(KindFieldStr); |
| 1871 |
switch (LateAttrParseKind(Kind)) { |
| 1872 |
#define CASE(X) \ |
| 1873 |
case LateAttrParseKind::X: \ |
| 1874 |
if (LAPK->getName().compare("LateAttrParse" #X) != 0) { \ |
| 1875 |
PrintFatalError( \ |
| 1876 |
Attr, \ |
| 1877 |
"Field `" + Twine(LateParsedStr) + "` set to `" + LAPK->getName() + \ |
| 1878 |
"` but this converts to `LateAttrParseKind::" + Twine(#X) + \ |
| 1879 |
"`"); \ |
| 1880 |
} \ |
| 1881 |
return LateAttrParseKind::X; |
| 1882 |
|
| 1883 |
CASE(Never) |
| 1884 |
CASE(Standard) |
| 1885 |
CASE(ExperimentalExt) |
| 1886 |
#undef CASE |
| 1887 |
} |
| 1888 |
|
| 1889 |
// The Kind value is completely invalid |
| 1890 |
auto KindValueStr = utostr(Kind); |
| 1891 |
PrintFatalError(Attr, "Field `" + Twine(LateParsedStr) + "` set to `" + |
| 1892 |
LAPK->getName() + "` has unexpected `" + |
| 1893 |
Twine(KindFieldStr) + "` value of " + KindValueStr); |
| 1894 |
} |
| 1895 |
|
| 1896 |
// Emits the LateParsed property for attributes. |
| 1897 |
static void emitClangAttrLateParsedListImpl(const RecordKeeper &Records, |
| 1898 |
raw_ostream &OS, |
| 1899 |
LateAttrParseKind LateParseMode) { |
| 1900 |
for (const auto *Attr : Records.getAllDerivedDefinitions("Attr")) { |
| 1901 |
if (LateAttrParseKind LateParsed = getLateAttrParseKind(Attr); |
| 1902 |
LateParsed != LateParseMode) |
| 1903 |
continue; |
| 1904 |
|
| 1905 |
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr); |
| 1906 |
|
| 1907 |
// FIXME: Handle non-GNU attributes |
| 1908 |
for (const auto &I : Spellings) { |
| 1909 |
if (I.variety() != "GNU") |
| 1910 |
continue; |
| 1911 |
OS << ".Case(\"" << I.name() << "\", 1)\n"; |
| 1912 |
} |
| 1913 |
} |
| 1914 |
} |
| 1915 |
|
| 1916 |
static void emitClangAttrLateParsedList(const RecordKeeper &Records, |
| 1917 |
raw_ostream &OS) { |
| 1918 |
OS << "#if defined(CLANG_ATTR_LATE_PARSED_LIST)\n"; |
| 1919 |
emitClangAttrLateParsedListImpl(Records, OS, LateAttrParseKind::Standard); |
| 1920 |
OS << "#endif // CLANG_ATTR_LATE_PARSED_LIST\n\n"; |
| 1921 |
} |
| 1922 |
|
| 1923 |
static void emitClangAttrLateParsedExperimentalList(const RecordKeeper &Records, |
| 1924 |
raw_ostream &OS) { |
| 1925 |
OS << "#if defined(CLANG_ATTR_LATE_PARSED_EXPERIMENTAL_EXT_LIST)\n"; |
| 1926 |
emitClangAttrLateParsedListImpl(Records, OS, |
| 1927 |
LateAttrParseKind::ExperimentalExt); |
| 1928 |
OS << "#endif // CLANG_ATTR_LATE_PARSED_EXPERIMENTAL_EXT_LIST\n\n"; |
| 1929 |
} |
| 1930 |
|
| 1931 |
static bool hasGNUorCXX11Spelling(const Record &Attribute) { |
| 1932 |
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attribute); |
| 1933 |
for (const auto &I : Spellings) { |
| 1934 |
if (I.variety() == "GNU" || I.variety() == "CXX11") |
| 1935 |
return true; |
| 1936 |
} |
| 1937 |
return false; |
| 1938 |
} |
| 1939 |
|
| 1940 |
namespace { |
| 1941 |
|
| 1942 |
struct AttributeSubjectMatchRule { |
| 1943 |
const Record *MetaSubject; |
| 1944 |
const Record *Constraint; |
| 1945 |
|
| 1946 |
AttributeSubjectMatchRule(const Record *MetaSubject, const Record *Constraint) |
| 1947 |
: MetaSubject(MetaSubject), Constraint(Constraint) { |
| 1948 |
assert(MetaSubject && "Missing subject"); |
| 1949 |
} |
| 1950 |
|
| 1951 |
bool isSubRule() const { return Constraint != nullptr; } |
| 1952 |
|
| 1953 |
std::vector<const Record *> getSubjects() const { |
| 1954 |
return (Constraint ? Constraint : MetaSubject) |
| 1955 |
->getValueAsListOfDefs("Subjects"); |
| 1956 |
} |
| 1957 |
|
| 1958 |
std::vector<const Record *> getLangOpts() const { |
| 1959 |
if (Constraint) { |
| 1960 |
// Lookup the options in the sub-rule first, in case the sub-rule |
| 1961 |
// overrides the rules options. |
| 1962 |
std::vector<const Record *> Opts = |
| 1963 |
Constraint->getValueAsListOfDefs("LangOpts"); |
| 1964 |
if (!Opts.empty()) |
| 1965 |
return Opts; |
| 1966 |
} |
| 1967 |
return MetaSubject->getValueAsListOfDefs("LangOpts"); |
| 1968 |
} |
| 1969 |
|
| 1970 |
// Abstract rules are used only for sub-rules |
| 1971 |
bool isAbstractRule() const { return getSubjects().empty(); } |
| 1972 |
|
| 1973 |
StringRef getName() const { |
| 1974 |
return (Constraint ? Constraint : MetaSubject)->getValueAsString("Name"); |
| 1975 |
} |
| 1976 |
|
| 1977 |
bool isNegatedSubRule() const { |
| 1978 |
assert(isSubRule() && "Not a sub-rule"); |
| 1979 |
return Constraint->getValueAsBit("Negated"); |
| 1980 |
} |
| 1981 |
|
| 1982 |
std::string getSpelling() const { |
| 1983 |
std::string Result = MetaSubject->getValueAsString("Name").str(); |
| 1984 |
if (isSubRule()) { |
| 1985 |
Result += '('; |
| 1986 |
if (isNegatedSubRule()) |
| 1987 |
Result += "unless("; |
| 1988 |
Result += getName(); |
| 1989 |
if (isNegatedSubRule()) |
| 1990 |
Result += ')'; |
| 1991 |
Result += ')'; |
| 1992 |
} |
| 1993 |
return Result; |
| 1994 |
} |
| 1995 |
|
| 1996 |
std::string getEnumValueName() const { |
| 1997 |
SmallString<128> Result; |
| 1998 |
Result += "SubjectMatchRule_"; |
| 1999 |
Result += MetaSubject->getValueAsString("Name"); |
| 2000 |
if (isSubRule()) { |
| 2001 |
Result += "_"; |
| 2002 |
if (isNegatedSubRule()) |
| 2003 |
Result += "not_"; |
| 2004 |
Result += Constraint->getValueAsString("Name"); |
| 2005 |
} |
| 2006 |
if (isAbstractRule()) |
| 2007 |
Result += "_abstract"; |
| 2008 |
return std::string(Result); |
| 2009 |
} |
| 2010 |
|
| 2011 |
std::string getEnumValue() const { return "attr::" + getEnumValueName(); } |
| 2012 |
|
| 2013 |
static const char *EnumName; |
| 2014 |
}; |
| 2015 |
|
| 2016 |
const char *AttributeSubjectMatchRule::EnumName = "attr::SubjectMatchRule"; |
| 2017 |
|
| 2018 |
struct PragmaClangAttributeSupport { |
| 2019 |
std::vector<AttributeSubjectMatchRule> Rules; |
| 2020 |
|
| 2021 |
class RuleOrAggregateRuleSet { |
| 2022 |
std::vector<AttributeSubjectMatchRule> Rules; |
| 2023 |
bool IsRule; |
| 2024 |
RuleOrAggregateRuleSet(ArrayRef<AttributeSubjectMatchRule> Rules, |
| 2025 |
bool IsRule) |
| 2026 |
: Rules(Rules), IsRule(IsRule) {} |
| 2027 |
|
| 2028 |
public: |
| 2029 |
bool isRule() const { return IsRule; } |
| 2030 |
|
| 2031 |
const AttributeSubjectMatchRule &getRule() const { |
| 2032 |
assert(IsRule && "not a rule!"); |
| 2033 |
return Rules[0]; |
| 2034 |
} |
| 2035 |
|
| 2036 |
ArrayRef<AttributeSubjectMatchRule> getAggregateRuleSet() const { |
| 2037 |
return Rules; |
| 2038 |
} |
| 2039 |
|
| 2040 |
static RuleOrAggregateRuleSet |
| 2041 |
getRule(const AttributeSubjectMatchRule &Rule) { |
| 2042 |
return RuleOrAggregateRuleSet(Rule, /*IsRule=*/true); |
| 2043 |
} |
| 2044 |
static RuleOrAggregateRuleSet |
| 2045 |
getAggregateRuleSet(ArrayRef<AttributeSubjectMatchRule> Rules) { |
| 2046 |
return RuleOrAggregateRuleSet(Rules, /*IsRule=*/false); |
| 2047 |
} |
| 2048 |
}; |
| 2049 |
DenseMap<const Record *, RuleOrAggregateRuleSet> SubjectsToRules; |
| 2050 |
|
| 2051 |
PragmaClangAttributeSupport(const RecordKeeper &Records); |
| 2052 |
|
| 2053 |
bool isAttributedSupported(const Record &Attribute); |
| 2054 |
|
| 2055 |
void emitMatchRuleList(raw_ostream &OS); |
| 2056 |
|
| 2057 |
void generateStrictConformsTo(const Record &Attr, raw_ostream &OS); |
| 2058 |
|
| 2059 |
void generateParsingHelpers(raw_ostream &OS); |
| 2060 |
}; |
| 2061 |
|
| 2062 |
} // end anonymous namespace |
| 2063 |
|
| 2064 |
static bool isSupportedPragmaClangAttributeSubject(const Record &Subject) { |
| 2065 |
// FIXME: #pragma clang attribute does not currently support statement |
| 2066 |
// attributes, so test whether the subject is one that appertains to a |
| 2067 |
// declaration node. However, it may be reasonable for support for statement |
| 2068 |
// attributes to be added. |
| 2069 |
if (Subject.isSubClassOf("DeclNode") || Subject.isSubClassOf("DeclBase") || |
| 2070 |
Subject.getName() == "DeclBase") |
| 2071 |
return true; |
| 2072 |
|
| 2073 |
if (Subject.isSubClassOf("SubsetSubject")) |
| 2074 |
return isSupportedPragmaClangAttributeSubject( |
| 2075 |
*Subject.getValueAsDef("Base")); |
| 2076 |
|
| 2077 |
return false; |
| 2078 |
} |
| 2079 |
|
| 2080 |
static bool doesDeclDeriveFrom(const Record *D, const Record *Base) { |
| 2081 |
const Record *CurrentBase = D->getValueAsOptionalDef(BaseFieldName); |
| 2082 |
if (!CurrentBase) |
| 2083 |
return false; |
| 2084 |
if (CurrentBase == Base) |
| 2085 |
return true; |
| 2086 |
return doesDeclDeriveFrom(CurrentBase, Base); |
| 2087 |
} |
| 2088 |
|
| 2089 |
PragmaClangAttributeSupport::PragmaClangAttributeSupport( |
| 2090 |
const RecordKeeper &Records) { |
| 2091 |
auto MapFromSubjectsToRules = [this](const Record *SubjectContainer, |
| 2092 |
const Record *MetaSubject, |
| 2093 |
const Record *Constraint) { |
| 2094 |
Rules.emplace_back(MetaSubject, Constraint); |
| 2095 |
for (const Record *Subject : |
| 2096 |
SubjectContainer->getValueAsListOfDefs("Subjects")) { |
| 2097 |
bool Inserted = |
| 2098 |
SubjectsToRules |
| 2099 |
.try_emplace(Subject, RuleOrAggregateRuleSet::getRule( |
| 2100 |
AttributeSubjectMatchRule(MetaSubject, |
| 2101 |
Constraint))) |
| 2102 |
.second; |
| 2103 |
if (!Inserted) { |
| 2104 |
PrintFatalError("Attribute subject match rules should not represent" |
| 2105 |
"same attribute subjects."); |
| 2106 |
} |
| 2107 |
} |
| 2108 |
}; |
| 2109 |
for (const auto *MetaSubject : |
| 2110 |
Records.getAllDerivedDefinitions("AttrSubjectMatcherRule")) { |
| 2111 |
MapFromSubjectsToRules(MetaSubject, MetaSubject, /*Constraints=*/nullptr); |
| 2112 |
for (const Record *Constraint : |
| 2113 |
MetaSubject->getValueAsListOfDefs("Constraints")) |
| 2114 |
MapFromSubjectsToRules(Constraint, MetaSubject, Constraint); |
| 2115 |
} |
| 2116 |
|
| 2117 |
ArrayRef<const Record *> DeclNodes = |
| 2118 |
Records.getAllDerivedDefinitions(DeclNodeClassName); |
| 2119 |
for (const auto *Aggregate : |
| 2120 |
Records.getAllDerivedDefinitions("AttrSubjectMatcherAggregateRule")) { |
| 2121 |
const Record *SubjectDecl = Aggregate->getValueAsDef("Subject"); |
| 2122 |
|
| 2123 |
// Gather sub-classes of the aggregate subject that act as attribute |
| 2124 |
// subject rules. |
| 2125 |
std::vector<AttributeSubjectMatchRule> Rules; |
| 2126 |
for (const auto *D : DeclNodes) { |
| 2127 |
if (doesDeclDeriveFrom(D, SubjectDecl)) { |
| 2128 |
auto It = SubjectsToRules.find(D); |
| 2129 |
if (It == SubjectsToRules.end()) |
| 2130 |
continue; |
| 2131 |
if (!It->second.isRule() || It->second.getRule().isSubRule()) |
| 2132 |
continue; // Assume that the rule will be included as well. |
| 2133 |
Rules.push_back(It->second.getRule()); |
| 2134 |
} |
| 2135 |
} |
| 2136 |
|
| 2137 |
bool Inserted = |
| 2138 |
SubjectsToRules |
| 2139 |
.try_emplace(SubjectDecl, |
| 2140 |
RuleOrAggregateRuleSet::getAggregateRuleSet(Rules)) |
| 2141 |
.second; |
| 2142 |
if (!Inserted) { |
| 2143 |
PrintFatalError("Attribute subject match rules should not represent" |
| 2144 |
"same attribute subjects."); |
| 2145 |
} |
| 2146 |
} |
| 2147 |
} |
| 2148 |
|
| 2149 |
static PragmaClangAttributeSupport & |
| 2150 |
getPragmaAttributeSupport(const RecordKeeper &Records) { |
| 2151 |
static PragmaClangAttributeSupport Instance(Records); |
| 2152 |
return Instance; |
| 2153 |
} |
| 2154 |
|
| 2155 |
void PragmaClangAttributeSupport::emitMatchRuleList(raw_ostream &OS) { |
| 2156 |
OS << "#ifndef ATTR_MATCH_SUB_RULE\n"; |
| 2157 |
OS << "#define ATTR_MATCH_SUB_RULE(Value, Spelling, IsAbstract, Parent, " |
| 2158 |
"IsNegated) " |
| 2159 |
<< "ATTR_MATCH_RULE(Value, Spelling, IsAbstract)\n"; |
| 2160 |
OS << "#endif\n"; |
| 2161 |
for (const auto &Rule : Rules) { |
| 2162 |
OS << (Rule.isSubRule() ? "ATTR_MATCH_SUB_RULE" : "ATTR_MATCH_RULE") << '('; |
| 2163 |
OS << Rule.getEnumValueName() << ", \"" << Rule.getSpelling() << "\", " |
| 2164 |
<< Rule.isAbstractRule(); |
| 2165 |
if (Rule.isSubRule()) |
| 2166 |
OS << ", " |
| 2167 |
<< AttributeSubjectMatchRule(Rule.MetaSubject, nullptr).getEnumValue() |
| 2168 |
<< ", " << Rule.isNegatedSubRule(); |
| 2169 |
OS << ")\n"; |
| 2170 |
} |
| 2171 |
OS << "#undef ATTR_MATCH_SUB_RULE\n"; |
| 2172 |
} |
| 2173 |
|
| 2174 |
bool PragmaClangAttributeSupport::isAttributedSupported( |
| 2175 |
const Record &Attribute) { |
| 2176 |
// If the attribute explicitly specified whether to support #pragma clang |
| 2177 |
// attribute, use that setting. |
| 2178 |
bool Unset; |
| 2179 |
bool SpecifiedResult = |
| 2180 |
Attribute.getValueAsBitOrUnset("PragmaAttributeSupport", Unset); |
| 2181 |
if (!Unset) |
| 2182 |
return SpecifiedResult; |
| 2183 |
|
| 2184 |
// Opt-out rules: |
| 2185 |
|
| 2186 |
// An attribute requires delayed parsing (LateParsed is on). |
| 2187 |
switch (getLateAttrParseKind(&Attribute)) { |
| 2188 |
case LateAttrParseKind::Never: |
| 2189 |
break; |
| 2190 |
case LateAttrParseKind::Standard: |
| 2191 |
return false; |
| 2192 |
case LateAttrParseKind::ExperimentalExt: |
| 2193 |
// This is only late parsed in certain parsing contexts when |
| 2194 |
// `LangOpts.ExperimentalLateParseAttributes` is true. Information about the |
| 2195 |
// parsing context and `LangOpts` is not available in this method so just |
| 2196 |
// opt this attribute out. |
| 2197 |
return false; |
| 2198 |
} |
| 2199 |
|
| 2200 |
// An attribute has no GNU/CXX11 spelling |
| 2201 |
if (!hasGNUorCXX11Spelling(Attribute)) |
| 2202 |
return false; |
| 2203 |
// An attribute subject list has a subject that isn't covered by one of the |
| 2204 |
// subject match rules or has no subjects at all. |
| 2205 |
if (Attribute.isValueUnset("Subjects")) |
| 2206 |
return false; |
| 2207 |
const Record *SubjectObj = Attribute.getValueAsDef("Subjects"); |
| 2208 |
bool HasAtLeastOneValidSubject = false; |
| 2209 |
for (const auto *Subject : SubjectObj->getValueAsListOfDefs("Subjects")) { |
| 2210 |
if (!isSupportedPragmaClangAttributeSubject(*Subject)) |
| 2211 |
continue; |
| 2212 |
if (!SubjectsToRules.contains(Subject)) |
| 2213 |
return false; |
| 2214 |
HasAtLeastOneValidSubject = true; |
| 2215 |
} |
| 2216 |
return HasAtLeastOneValidSubject; |
| 2217 |
} |
| 2218 |
|
| 2219 |
static std::string GenerateTestExpression(ArrayRef<const Record *> LangOpts) { |
| 2220 |
std::string Test; |
| 2221 |
|
| 2222 |
for (auto *E : LangOpts) { |
| 2223 |
if (!Test.empty()) |
| 2224 |
Test += " || "; |
| 2225 |
|
| 2226 |
const StringRef Code = E->getValueAsString("CustomCode"); |
| 2227 |
if (!Code.empty()) { |
| 2228 |
Test += "("; |
| 2229 |
Test += Code; |
| 2230 |
Test += ")"; |
| 2231 |
if (!E->getValueAsString("Name").empty()) { |
| 2232 |
PrintWarning( |
| 2233 |
E->getLoc(), |
| 2234 |
"non-empty 'Name' field ignored because 'CustomCode' was supplied"); |
| 2235 |
} |
| 2236 |
} else { |
| 2237 |
Test += "LangOpts."; |
| 2238 |
Test += E->getValueAsString("Name"); |
| 2239 |
} |
| 2240 |
} |
| 2241 |
|
| 2242 |
if (Test.empty()) |
| 2243 |
return "true"; |
| 2244 |
|
| 2245 |
return Test; |
| 2246 |
} |
| 2247 |
|
| 2248 |
void |
| 2249 |
PragmaClangAttributeSupport::generateStrictConformsTo(const Record &Attr, |
| 2250 |
raw_ostream &OS) { |
| 2251 |
if (!isAttributedSupported(Attr) || Attr.isValueUnset("Subjects")) |
| 2252 |
return; |
| 2253 |
// Generate a function that constructs a set of matching rules that describe |
| 2254 |
// to which declarations the attribute should apply to. |
| 2255 |
OS << "void getPragmaAttributeMatchRules(" |
| 2256 |
<< "llvm::SmallVectorImpl<std::pair<" |
| 2257 |
<< AttributeSubjectMatchRule::EnumName |
| 2258 |
<< ", bool>> &MatchRules, const LangOptions &LangOpts) const override {\n"; |
| 2259 |
const Record *SubjectObj = Attr.getValueAsDef("Subjects"); |
| 2260 |
for (const auto *Subject : SubjectObj->getValueAsListOfDefs("Subjects")) { |
| 2261 |
if (!isSupportedPragmaClangAttributeSubject(*Subject)) |
| 2262 |
continue; |
| 2263 |
auto It = SubjectsToRules.find(Subject); |
| 2264 |
assert(It != SubjectsToRules.end() && |
| 2265 |
"This attribute is unsupported by #pragma clang attribute"); |
| 2266 |
for (const auto &Rule : It->getSecond().getAggregateRuleSet()) { |
| 2267 |
// The rule might be language specific, so only subtract it from the given |
| 2268 |
// rules if the specific language options are specified. |
| 2269 |
std::vector<const Record *> LangOpts = Rule.getLangOpts(); |
| 2270 |
OS << " MatchRules.push_back(std::make_pair(" << Rule.getEnumValue() |
| 2271 |
<< ", /*IsSupported=*/" << GenerateTestExpression(LangOpts) |
| 2272 |
<< "));\n"; |
| 2273 |
} |
| 2274 |
} |
| 2275 |
OS << "}\n\n"; |
| 2276 |
} |
| 2277 |
|
| 2278 |
void PragmaClangAttributeSupport::generateParsingHelpers(raw_ostream &OS) { |
| 2279 |
// Generate routines that check the names of sub-rules. |
| 2280 |
OS << "std::optional<attr::SubjectMatchRule> " |
| 2281 |
"defaultIsAttributeSubjectMatchSubRuleFor(StringRef, bool) {\n"; |
| 2282 |
OS << " return std::nullopt;\n"; |
| 2283 |
OS << "}\n\n"; |
| 2284 |
|
| 2285 |
MapVector<const Record *, std::vector<AttributeSubjectMatchRule>> |
| 2286 |
SubMatchRules; |
| 2287 |
for (const auto &Rule : Rules) { |
| 2288 |
if (!Rule.isSubRule()) |
| 2289 |
continue; |
| 2290 |
SubMatchRules[Rule.MetaSubject].push_back(Rule); |
| 2291 |
} |
| 2292 |
|
| 2293 |
for (const auto &SubMatchRule : SubMatchRules) { |
| 2294 |
OS << "std::optional<attr::SubjectMatchRule> " |
| 2295 |
"isAttributeSubjectMatchSubRuleFor_" |
| 2296 |
<< SubMatchRule.first->getValueAsString("Name") |
| 2297 |
<< "(StringRef Name, bool IsUnless) {\n"; |
| 2298 |
OS << " if (IsUnless)\n"; |
| 2299 |
OS << " return " |
| 2300 |
"llvm::StringSwitch<std::optional<attr::SubjectMatchRule>>(Name).\n"; |
| 2301 |
for (const auto &Rule : SubMatchRule.second) { |
| 2302 |
if (Rule.isNegatedSubRule()) |
| 2303 |
OS << " Case(\"" << Rule.getName() << "\", " << Rule.getEnumValue() |
| 2304 |
<< ").\n"; |
| 2305 |
} |
| 2306 |
OS << " Default(std::nullopt);\n"; |
| 2307 |
OS << " return " |
| 2308 |
"llvm::StringSwitch<std::optional<attr::SubjectMatchRule>>(Name).\n"; |
| 2309 |
for (const auto &Rule : SubMatchRule.second) { |
| 2310 |
if (!Rule.isNegatedSubRule()) |
| 2311 |
OS << " Case(\"" << Rule.getName() << "\", " << Rule.getEnumValue() |
| 2312 |
<< ").\n"; |
| 2313 |
} |
| 2314 |
OS << " Default(std::nullopt);\n"; |
| 2315 |
OS << "}\n\n"; |
| 2316 |
} |
| 2317 |
|
| 2318 |
// Generate the function that checks for the top-level rules. |
| 2319 |
OS << "std::pair<std::optional<attr::SubjectMatchRule>, " |
| 2320 |
"std::optional<attr::SubjectMatchRule> (*)(StringRef, " |
| 2321 |
"bool)> isAttributeSubjectMatchRule(StringRef Name) {\n"; |
| 2322 |
OS << " return " |
| 2323 |
"llvm::StringSwitch<std::pair<std::optional<attr::SubjectMatchRule>, " |
| 2324 |
"std::optional<attr::SubjectMatchRule> (*) (StringRef, " |
| 2325 |
"bool)>>(Name).\n"; |
| 2326 |
for (const auto &Rule : Rules) { |
| 2327 |
if (Rule.isSubRule()) |
| 2328 |
continue; |
| 2329 |
std::string SubRuleFunction; |
| 2330 |
if (SubMatchRules.count(Rule.MetaSubject)) |
| 2331 |
SubRuleFunction = |
| 2332 |
("isAttributeSubjectMatchSubRuleFor_" + Rule.getName()).str(); |
| 2333 |
else |
| 2334 |
SubRuleFunction = "defaultIsAttributeSubjectMatchSubRuleFor"; |
| 2335 |
OS << " Case(\"" << Rule.getName() << "\", std::make_pair(" |
| 2336 |
<< Rule.getEnumValue() << ", " << SubRuleFunction << ")).\n"; |
| 2337 |
} |
| 2338 |
OS << " Default(std::make_pair(std::nullopt, " |
| 2339 |
"defaultIsAttributeSubjectMatchSubRuleFor));\n"; |
| 2340 |
OS << "}\n\n"; |
| 2341 |
|
| 2342 |
// Generate the function that checks for the submatch rules. |
| 2343 |
OS << "const char *validAttributeSubjectMatchSubRules(" |
| 2344 |
<< AttributeSubjectMatchRule::EnumName << " Rule) {\n"; |
| 2345 |
OS << " switch (Rule) {\n"; |
| 2346 |
for (const auto &SubMatchRule : SubMatchRules) { |
| 2347 |
OS << " case " |
| 2348 |
<< AttributeSubjectMatchRule(SubMatchRule.first, nullptr).getEnumValue() |
| 2349 |
<< ":\n"; |
| 2350 |
OS << " return \"'"; |
| 2351 |
bool IsFirst = true; |
| 2352 |
for (const auto &Rule : SubMatchRule.second) { |
| 2353 |
if (!IsFirst) |
| 2354 |
OS << ", '"; |
| 2355 |
IsFirst = false; |
| 2356 |
if (Rule.isNegatedSubRule()) |
| 2357 |
OS << "unless("; |
| 2358 |
OS << Rule.getName(); |
| 2359 |
if (Rule.isNegatedSubRule()) |
| 2360 |
OS << ')'; |
| 2361 |
OS << "'"; |
| 2362 |
} |
| 2363 |
OS << "\";\n"; |
| 2364 |
} |
| 2365 |
OS << " default: return nullptr;\n"; |
| 2366 |
OS << " }\n"; |
| 2367 |
OS << "}\n\n"; |
| 2368 |
} |
| 2369 |
|
| 2370 |
template <typename Fn> static void forEachSpelling(const Record &Attr, Fn &&F) { |
| 2371 |
for (const FlattenedSpelling &S : GetFlattenedSpellings(Attr)) { |
| 2372 |
F(S); |
| 2373 |
} |
| 2374 |
} |
| 2375 |
|
| 2376 |
static std::map<StringRef, std::vector<const Record *>> NameToAttrsMap; |
| 2377 |
|
| 2378 |
/// Build a map from the attribute name to the Attrs that use that name. If more |
| 2379 |
/// than one Attr use a name, the arguments could be different so a more complex |
| 2380 |
/// check is needed in the generated switch. |
| 2381 |
static void generateNameToAttrsMap(const RecordKeeper &Records) { |
| 2382 |
for (const auto *A : Records.getAllDerivedDefinitions("Attr")) { |
| 2383 |
for (const FlattenedSpelling &S : GetFlattenedSpellings(*A)) { |
| 2384 |
auto [It, Inserted] = NameToAttrsMap.try_emplace(S.name()); |
| 2385 |
if (Inserted || !is_contained(It->second, A)) |
| 2386 |
It->second.emplace_back(A); |
| 2387 |
} |
| 2388 |
} |
| 2389 |
} |
| 2390 |
|
| 2391 |
/// Generate the info needed to produce the case values in case more than one |
| 2392 |
/// attribute has the same name. Store the info in a map that can be processed |
| 2393 |
/// after all attributes are seen. |
| 2394 |
static void generateFlattenedSpellingInfo(const Record &Attr, |
| 2395 |
std::map<StringRef, FSIVecTy> &Map, |
| 2396 |
uint32_t ArgMask = 0) { |
| 2397 |
std::string TargetTest; |
| 2398 |
if (Attr.isSubClassOf("TargetSpecificAttr") && |
| 2399 |
!Attr.isValueUnset("ParseKind")) { |
| 2400 |
const Record *T = Attr.getValueAsDef("Target"); |
| 2401 |
std::vector<StringRef> Arches = T->getValueAsListOfStrings("Arches"); |
| 2402 |
(void)GenerateTargetSpecificAttrChecks(T, Arches, TargetTest, nullptr); |
| 2403 |
} |
| 2404 |
|
| 2405 |
forEachSpelling(Attr, [&](const FlattenedSpelling &S) { |
| 2406 |
Map[S.name()].emplace_back(S.variety(), S.nameSpace(), TargetTest, ArgMask); |
| 2407 |
}); |
| 2408 |
} |
| 2409 |
|
| 2410 |
static bool nameAppliesToOneAttribute(StringRef Name) { |
| 2411 |
auto It = NameToAttrsMap.find(Name); |
| 2412 |
assert(It != NameToAttrsMap.end()); |
| 2413 |
return It->second.size() == 1; |
| 2414 |
} |
| 2415 |
|
| 2416 |
static bool emitIfSimpleValue(StringRef Name, uint32_t ArgMask, |
| 2417 |
raw_ostream &OS) { |
| 2418 |
if (nameAppliesToOneAttribute(Name)) { |
| 2419 |
OS << ".Case(\"" << Name << "\", "; |
| 2420 |
if (ArgMask != 0) |
| 2421 |
OS << ArgMask << ")\n"; |
| 2422 |
else |
| 2423 |
OS << "true)\n"; |
| 2424 |
return true; |
| 2425 |
} |
| 2426 |
return false; |
| 2427 |
} |
| 2428 |
|
| 2429 |
static void emitSingleCondition(const FlattenedSpellingInfo &FSI, |
| 2430 |
raw_ostream &OS) { |
| 2431 |
OS << "(Syntax==AttributeCommonInfo::AS_" << FSI.Syntax << " && "; |
| 2432 |
if (!FSI.Scope.empty()) |
| 2433 |
OS << "ScopeName && ScopeName->getName()==\"" << FSI.Scope << "\""; |
| 2434 |
else |
| 2435 |
OS << "!ScopeName"; |
| 2436 |
if (!FSI.TargetTest.empty()) |
| 2437 |
OS << " && " << FSI.TargetTest; |
| 2438 |
OS << ")"; |
| 2439 |
} |
| 2440 |
|
| 2441 |
static void emitStringSwitchCases(std::map<StringRef, FSIVecTy> &Map, |
| 2442 |
raw_ostream &OS) { |
| 2443 |
for (const auto &[Name, Vec] : Map) { |
| 2444 |
if (emitIfSimpleValue(Name, Vec[0].ArgMask, OS)) |
| 2445 |
continue; |
| 2446 |
|
| 2447 |
// Not simple, build expressions for each case. |
| 2448 |
OS << ".Case(\"" << Name << "\", "; |
| 2449 |
for (unsigned I = 0, E = Vec.size(); I < E; ++I) { |
| 2450 |
emitSingleCondition(Vec[I], OS); |
| 2451 |
uint32_t ArgMask = Vec[I].ArgMask; |
| 2452 |
if (E == 1 && ArgMask == 0) |
| 2453 |
continue; |
| 2454 |
|
| 2455 |
// More than one or it's the Mask form. Create a conditional expression. |
| 2456 |
uint32_t SuccessValue = ArgMask != 0 ? ArgMask : 1; |
| 2457 |
OS << " ? " << SuccessValue << " : "; |
| 2458 |
if (I == E - 1) |
| 2459 |
OS << 0; |
| 2460 |
} |
| 2461 |
OS << ")\n"; |
| 2462 |
} |
| 2463 |
} |
| 2464 |
|
| 2465 |
static bool isTypeArgument(const Record *Arg) { |
| 2466 |
return !Arg->getDirectSuperClasses().empty() && |
| 2467 |
Arg->getDirectSuperClasses().back().first->getName() == "TypeArgument"; |
| 2468 |
} |
| 2469 |
|
| 2470 |
/// Emits the first-argument-is-type property for attributes. |
| 2471 |
static void emitClangAttrTypeArgList(const RecordKeeper &Records, |
| 2472 |
raw_ostream &OS) { |
| 2473 |
OS << "#if defined(CLANG_ATTR_TYPE_ARG_LIST)\n"; |
| 2474 |
std::map<StringRef, FSIVecTy> FSIMap; |
| 2475 |
for (const auto *Attr : Records.getAllDerivedDefinitions("Attr")) { |
| 2476 |
// Determine whether the first argument is a type. |
| 2477 |
std::vector<const Record *> Args = Attr->getValueAsListOfDefs("Args"); |
| 2478 |
if (Args.empty()) |
| 2479 |
continue; |
| 2480 |
|
| 2481 |
if (!isTypeArgument(Args[0])) |
| 2482 |
continue; |
| 2483 |
generateFlattenedSpellingInfo(*Attr, FSIMap); |
| 2484 |
} |
| 2485 |
emitStringSwitchCases(FSIMap, OS); |
| 2486 |
OS << "#endif // CLANG_ATTR_TYPE_ARG_LIST\n\n"; |
| 2487 |
} |
| 2488 |
|
| 2489 |
/// Emits the parse-arguments-in-unevaluated-context property for |
| 2490 |
/// attributes. |
| 2491 |
static void emitClangAttrArgContextList(const RecordKeeper &Records, |
| 2492 |
raw_ostream &OS) { |
| 2493 |
OS << "#if defined(CLANG_ATTR_ARG_CONTEXT_LIST)\n"; |
| 2494 |
std::map<StringRef, FSIVecTy> FSIMap; |
| 2495 |
ParsedAttrMap Attrs = getParsedAttrList(Records); |
| 2496 |
for (const auto &I : Attrs) { |
| 2497 |
const Record &Attr = *I.second; |
| 2498 |
|
| 2499 |
if (!Attr.getValueAsBit("ParseArgumentsAsUnevaluated")) |
| 2500 |
continue; |
| 2501 |
generateFlattenedSpellingInfo(Attr, FSIMap); |
| 2502 |
} |
| 2503 |
emitStringSwitchCases(FSIMap, OS); |
| 2504 |
OS << "#endif // CLANG_ATTR_ARG_CONTEXT_LIST\n\n"; |
| 2505 |
} |
| 2506 |
|
| 2507 |
static bool isIdentifierArgument(const Record *Arg) { |
| 2508 |
return !Arg->getDirectSuperClasses().empty() && |
| 2509 |
StringSwitch<bool>( |
| 2510 |
Arg->getDirectSuperClasses().back().first->getName()) |
| 2511 |
.Case("IdentifierArgument", true) |
| 2512 |
.Case("EnumArgument", true) |
| 2513 |
.Case("VariadicEnumArgument", true) |
| 2514 |
.Default(false); |
| 2515 |
} |
| 2516 |
|
| 2517 |
static bool isVariadicIdentifierArgument(const Record *Arg) { |
| 2518 |
return !Arg->getDirectSuperClasses().empty() && |
| 2519 |
StringSwitch<bool>( |
| 2520 |
Arg->getDirectSuperClasses().back().first->getName()) |
| 2521 |
.Case("VariadicIdentifierArgument", true) |
| 2522 |
.Case("VariadicParamOrParamIdxArgument", true) |
| 2523 |
.Default(false); |
| 2524 |
} |
| 2525 |
|
| 2526 |
static bool isVariadicExprArgument(const Record *Arg) { |
| 2527 |
return !Arg->getDirectSuperClasses().empty() && |
| 2528 |
StringSwitch<bool>( |
| 2529 |
Arg->getDirectSuperClasses().back().first->getName()) |
| 2530 |
.Case("VariadicExprArgument", true) |
| 2531 |
.Default(false); |
| 2532 |
} |
| 2533 |
|
| 2534 |
static bool isStringLiteralArgument(const Record *Arg) { |
| 2535 |
if (Arg->getDirectSuperClasses().empty()) |
| 2536 |
return false; |
| 2537 |
StringRef ArgKind = Arg->getDirectSuperClasses().back().first->getName(); |
| 2538 |
if (ArgKind == "EnumArgument") |
| 2539 |
return Arg->getValueAsBit("IsString"); |
| 2540 |
return ArgKind == "StringArgument"; |
| 2541 |
} |
| 2542 |
|
| 2543 |
static bool isVariadicStringLiteralArgument(const Record *Arg) { |
| 2544 |
if (Arg->getDirectSuperClasses().empty()) |
| 2545 |
return false; |
| 2546 |
StringRef ArgKind = Arg->getDirectSuperClasses().back().first->getName(); |
| 2547 |
if (ArgKind == "VariadicEnumArgument") |
| 2548 |
return Arg->getValueAsBit("IsString"); |
| 2549 |
return ArgKind == "VariadicStringArgument"; |
| 2550 |
} |
| 2551 |
|
| 2552 |
static void emitClangAttrVariadicIdentifierArgList(const RecordKeeper &Records, |
| 2553 |
raw_ostream &OS) { |
| 2554 |
OS << "#if defined(CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST)\n"; |
| 2555 |
std::map<StringRef, FSIVecTy> FSIMap; |
| 2556 |
for (const auto *A : Records.getAllDerivedDefinitions("Attr")) { |
| 2557 |
// Determine whether the first argument is a variadic identifier. |
| 2558 |
std::vector<const Record *> Args = A->getValueAsListOfDefs("Args"); |
| 2559 |
if (Args.empty() || !isVariadicIdentifierArgument(Args[0])) |
| 2560 |
continue; |
| 2561 |
generateFlattenedSpellingInfo(*A, FSIMap); |
| 2562 |
} |
| 2563 |
emitStringSwitchCases(FSIMap, OS); |
| 2564 |
OS << "#endif // CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST\n\n"; |
| 2565 |
} |
| 2566 |
|
| 2567 |
// Emits the list of arguments that should be parsed as unevaluated string |
| 2568 |
// literals for each attribute. |
| 2569 |
static void |
| 2570 |
emitClangAttrUnevaluatedStringLiteralList(const RecordKeeper &Records, |
| 2571 |
raw_ostream &OS) { |
| 2572 |
OS << "#if defined(CLANG_ATTR_STRING_LITERAL_ARG_LIST)\n"; |
| 2573 |
|
| 2574 |
auto MakeMask = [](ArrayRef<const Record *> Args) { |
| 2575 |
uint32_t Bits = 0; |
| 2576 |
assert(Args.size() <= 32 && "unsupported number of arguments in attribute"); |
| 2577 |
for (uint32_t N = 0; N < Args.size(); ++N) { |
| 2578 |
Bits |= (isStringLiteralArgument(Args[N]) << N); |
| 2579 |
// If we have a variadic string argument, set all the remaining bits to 1 |
| 2580 |
if (isVariadicStringLiteralArgument(Args[N])) { |
| 2581 |
Bits |= maskTrailingZeros<decltype(Bits)>(N); |
| 2582 |
break; |
| 2583 |
} |
| 2584 |
} |
| 2585 |
return Bits; |
| 2586 |
}; |
| 2587 |
|
| 2588 |
std::map<StringRef, FSIVecTy> FSIMap; |
| 2589 |
for (const auto *Attr : Records.getAllDerivedDefinitions("Attr")) { |
| 2590 |
// Determine whether there are any string arguments. |
| 2591 |
uint32_t ArgMask = MakeMask(Attr->getValueAsListOfDefs("Args")); |
| 2592 |
if (!ArgMask) |
| 2593 |
continue; |
| 2594 |
generateFlattenedSpellingInfo(*Attr, FSIMap, ArgMask); |
| 2595 |
} |
| 2596 |
emitStringSwitchCases(FSIMap, OS); |
| 2597 |
OS << "#endif // CLANG_ATTR_STRING_LITERAL_ARG_LIST\n\n"; |
| 2598 |
} |
| 2599 |
|
| 2600 |
// Emits the first-argument-is-identifier property for attributes. |
| 2601 |
static void emitClangAttrIdentifierArgList(const RecordKeeper &Records, |
| 2602 |
raw_ostream &OS) { |
| 2603 |
OS << "#if defined(CLANG_ATTR_IDENTIFIER_ARG_LIST)\n"; |
| 2604 |
std::map<StringRef, FSIVecTy> FSIMap; |
| 2605 |
for (const auto *Attr : Records.getAllDerivedDefinitions("Attr")) { |
| 2606 |
// Determine whether the first argument is an identifier. |
| 2607 |
std::vector<const Record *> Args = Attr->getValueAsListOfDefs("Args"); |
| 2608 |
if (Args.empty() || !isIdentifierArgument(Args[0])) |
| 2609 |
continue; |
| 2610 |
generateFlattenedSpellingInfo(*Attr, FSIMap); |
| 2611 |
} |
| 2612 |
emitStringSwitchCases(FSIMap, OS); |
| 2613 |
OS << "#endif // CLANG_ATTR_IDENTIFIER_ARG_LIST\n\n"; |
| 2614 |
} |
| 2615 |
|
| 2616 |
// Emits the list for attributes having StrictEnumParameters. |
| 2617 |
static void emitClangAttrStrictIdentifierArgList(const RecordKeeper &Records, |
| 2618 |
raw_ostream &OS) { |
| 2619 |
OS << "#if defined(CLANG_ATTR_STRICT_IDENTIFIER_ARG_LIST)\n"; |
| 2620 |
std::map<StringRef, FSIVecTy> FSIMap; |
| 2621 |
for (const auto *Attr : Records.getAllDerivedDefinitions("Attr")) { |
| 2622 |
if (!Attr->getValueAsBit("StrictEnumParameters")) |
| 2623 |
continue; |
| 2624 |
// Check that there is really an identifier argument. |
| 2625 |
std::vector<const Record *> Args = Attr->getValueAsListOfDefs("Args"); |
| 2626 |
if (none_of(Args, [&](const Record *R) { return isIdentifierArgument(R); })) |
| 2627 |
continue; |
| 2628 |
generateFlattenedSpellingInfo(*Attr, FSIMap); |
| 2629 |
} |
| 2630 |
emitStringSwitchCases(FSIMap, OS); |
| 2631 |
OS << "#endif // CLANG_ATTR_STRICT_IDENTIFIER_ARG_LIST\n\n"; |
| 2632 |
} |
| 2633 |
|
| 2634 |
static bool keywordThisIsaIdentifierInArgument(const Record *Arg) { |
| 2635 |
return !Arg->getDirectSuperClasses().empty() && |
| 2636 |
StringSwitch<bool>( |
| 2637 |
Arg->getDirectSuperClasses().back().first->getName()) |
| 2638 |
.Case("VariadicParamOrParamIdxArgument", true) |
| 2639 |
.Default(false); |
| 2640 |
} |
| 2641 |
|
| 2642 |
static void emitClangAttrThisIsaIdentifierArgList(const RecordKeeper &Records, |
| 2643 |
raw_ostream &OS) { |
| 2644 |
OS << "#if defined(CLANG_ATTR_THIS_ISA_IDENTIFIER_ARG_LIST)\n"; |
| 2645 |
std::map<StringRef, FSIVecTy> FSIMap; |
| 2646 |
for (const auto *A : Records.getAllDerivedDefinitions("Attr")) { |
| 2647 |
// Determine whether the first argument is a variadic identifier. |
| 2648 |
std::vector<const Record *> Args = A->getValueAsListOfDefs("Args"); |
| 2649 |
if (Args.empty() || !keywordThisIsaIdentifierInArgument(Args[0])) |
| 2650 |
continue; |
| 2651 |
generateFlattenedSpellingInfo(*A, FSIMap); |
| 2652 |
} |
| 2653 |
emitStringSwitchCases(FSIMap, OS); |
| 2654 |
OS << "#endif // CLANG_ATTR_THIS_ISA_IDENTIFIER_ARG_LIST\n\n"; |
| 2655 |
} |
| 2656 |
|
| 2657 |
static void emitClangAttrAcceptsExprPack(const RecordKeeper &Records, |
| 2658 |
raw_ostream &OS) { |
| 2659 |
OS << "#if defined(CLANG_ATTR_ACCEPTS_EXPR_PACK)\n"; |
| 2660 |
ParsedAttrMap Attrs = getParsedAttrList(Records); |
| 2661 |
std::map<StringRef, FSIVecTy> FSIMap; |
| 2662 |
for (const auto &I : Attrs) { |
| 2663 |
const Record &Attr = *I.second; |
| 2664 |
|
| 2665 |
if (!Attr.getValueAsBit("AcceptsExprPack")) |
| 2666 |
continue; |
| 2667 |
generateFlattenedSpellingInfo(Attr, FSIMap); |
| 2668 |
} |
| 2669 |
emitStringSwitchCases(FSIMap, OS); |
| 2670 |
OS << "#endif // CLANG_ATTR_ACCEPTS_EXPR_PACK\n\n"; |
| 2671 |
} |
| 2672 |
|
| 2673 |
static bool isRegularKeywordAttribute(const FlattenedSpelling &S) { |
| 2674 |
return (S.variety() == "Keyword" && |
| 2675 |
!S.getSpellingRecord().getValueAsBit("HasOwnParseRules")); |
| 2676 |
} |
| 2677 |
|
| 2678 |
static void emitFormInitializer(raw_ostream &OS, |
| 2679 |
const FlattenedSpelling &Spelling, |
| 2680 |
StringRef SpellingIndex) { |
| 2681 |
bool IsAlignas = |
| 2682 |
(Spelling.variety() == "Keyword" && Spelling.name() == "alignas"); |
| 2683 |
OS << "{AttributeCommonInfo::AS_" << Spelling.variety() << ", " |
| 2684 |
<< SpellingIndex << ", " << (IsAlignas ? "true" : "false") |
| 2685 |
<< " /*IsAlignas*/, " |
| 2686 |
<< (isRegularKeywordAttribute(Spelling) ? "true" : "false") |
| 2687 |
<< " /*IsRegularKeywordAttribute*/}"; |
| 2688 |
} |
| 2689 |
|
| 2690 |
static void emitAttributes(const RecordKeeper &Records, raw_ostream &OS, |
| 2691 |
bool Header) { |
| 2692 |
ParsedAttrMap AttrMap = getParsedAttrList(Records); |
| 2693 |
|
| 2694 |
// Helper to print the starting character of an attribute argument. If there |
| 2695 |
// hasn't been an argument yet, it prints an opening parenthese; otherwise it |
| 2696 |
// prints a comma. |
| 2697 |
OS << "static inline void DelimitAttributeArgument(" |
| 2698 |
<< "raw_ostream& OS, bool& IsFirst) {\n" |
| 2699 |
<< " if (IsFirst) {\n" |
| 2700 |
<< " IsFirst = false;\n" |
| 2701 |
<< " OS << \"(\";\n" |
| 2702 |
<< " } else\n" |
| 2703 |
<< " OS << \", \";\n" |
| 2704 |
<< "}\n"; |
| 2705 |
|
| 2706 |
for (const auto *Attr : Records.getAllDerivedDefinitions("Attr")) { |
| 2707 |
const Record &R = *Attr; |
| 2708 |
|
| 2709 |
// FIXME: Currently, documentation is generated as-needed due to the fact |
| 2710 |
// that there is no way to allow a generated project "reach into" the docs |
| 2711 |
// directory (for instance, it may be an out-of-tree build). However, we want |
| 2712 |
// to ensure that every attribute has a Documentation field, and produce an |
| 2713 |
// error if it has been neglected. Otherwise, the on-demand generation which |
| 2714 |
// happens server-side will fail. This code is ensuring that functionality, |
| 2715 |
// even though this Emitter doesn't technically need the documentation. |
| 2716 |
// When attribute documentation can be generated as part of the build |
| 2717 |
// itself, this code can be removed. |
| 2718 |
(void)R.getValueAsListOfDefs("Documentation"); |
| 2719 |
|
| 2720 |
if (!R.getValueAsBit("ASTNode")) |
| 2721 |
continue; |
| 2722 |
|
| 2723 |
std::vector<const Record *> Supers = R.getSuperClasses(); |
| 2724 |
assert(!Supers.empty() && "Forgot to specify a superclass for the attr"); |
| 2725 |
std::string SuperName; |
| 2726 |
bool Inheritable = false; |
| 2727 |
for (const Record *R : reverse(Supers)) { |
| 2728 |
if (R->getName() != "TargetSpecificAttr" && |
| 2729 |
R->getName() != "DeclOrTypeAttr" && SuperName.empty()) |
| 2730 |
SuperName = R->getName().str(); |
| 2731 |
if (R->getName() == "InheritableAttr") |
| 2732 |
Inheritable = true; |
| 2733 |
} |
| 2734 |
|
| 2735 |
if (Header) |
| 2736 |
OS << "class CLANG_ABI " << R.getName() << "Attr : public " << SuperName |
| 2737 |
<< " {\n"; |
| 2738 |
else |
| 2739 |
OS << "\n// " << R.getName() << "Attr implementation\n\n"; |
| 2740 |
|
| 2741 |
std::vector<const Record *> ArgRecords = R.getValueAsListOfDefs("Args"); |
| 2742 |
std::vector<std::unique_ptr<Argument>> Args; |
| 2743 |
Args.reserve(ArgRecords.size()); |
| 2744 |
|
| 2745 |
bool AttrAcceptsExprPack = Attr->getValueAsBit("AcceptsExprPack"); |
| 2746 |
if (AttrAcceptsExprPack) { |
| 2747 |
for (size_t I = 0; I < ArgRecords.size(); ++I) { |
| 2748 |
const Record *ArgR = ArgRecords[I]; |
| 2749 |
if (isIdentifierArgument(ArgR) || isVariadicIdentifierArgument(ArgR) || |
| 2750 |
isTypeArgument(ArgR)) |
| 2751 |
PrintFatalError(Attr->getLoc(), |
| 2752 |
"Attributes accepting packs cannot also " |
| 2753 |
"have identifier or type arguments."); |
| 2754 |
// When trying to determine if value-dependent expressions can populate |
| 2755 |
// the attribute without prior instantiation, the decision is made based |
| 2756 |
// on the assumption that only the last argument is ever variadic. |
| 2757 |
if (I < (ArgRecords.size() - 1) && isVariadicExprArgument(ArgR)) |
| 2758 |
PrintFatalError(Attr->getLoc(), |
| 2759 |
"Attributes accepting packs can only have the last " |
| 2760 |
"argument be variadic."); |
| 2761 |
} |
| 2762 |
} |
| 2763 |
|
| 2764 |
bool HasOptArg = false; |
| 2765 |
bool HasFakeArg = false; |
| 2766 |
for (const auto *ArgRecord : ArgRecords) { |
| 2767 |
Args.emplace_back(createArgument(*ArgRecord, R.getName())); |
| 2768 |
if (Header) { |
| 2769 |
Args.back()->writeDeclarations(OS); |
| 2770 |
OS << "\n\n"; |
| 2771 |
} |
| 2772 |
|
| 2773 |
// For these purposes, fake takes priority over optional. |
| 2774 |
if (Args.back()->isFake()) { |
| 2775 |
HasFakeArg = true; |
| 2776 |
} else if (Args.back()->isOptional()) { |
| 2777 |
HasOptArg = true; |
| 2778 |
} |
| 2779 |
} |
| 2780 |
|
| 2781 |
std::unique_ptr<VariadicExprArgument> DelayedArgs = nullptr; |
| 2782 |
if (AttrAcceptsExprPack) { |
| 2783 |
DelayedArgs = |
| 2784 |
std::make_unique<VariadicExprArgument>("DelayedArgs", R.getName()); |
| 2785 |
if (Header) { |
| 2786 |
DelayedArgs->writeDeclarations(OS); |
| 2787 |
OS << "\n\n"; |
| 2788 |
} |
| 2789 |
} |
| 2790 |
|
| 2791 |
if (Header) |
| 2792 |
OS << "public:\n"; |
| 2793 |
|
| 2794 |
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R); |
| 2795 |
|
| 2796 |
// If there are zero or one spellings, all spelling-related functionality |
| 2797 |
// can be elided. If all of the spellings share the same name, the spelling |
| 2798 |
// functionality can also be elided. |
| 2799 |
bool ElideSpelling = (Spellings.size() <= 1) || |
| 2800 |
SpellingNamesAreCommon(Spellings); |
| 2801 |
|
| 2802 |
// This maps spelling index values to semantic Spelling enumerants. |
| 2803 |
SemanticSpellingMap SemanticToSyntacticMap; |
| 2804 |
|
| 2805 |
std::string SpellingEnum; |
| 2806 |
if (Spellings.size() > 1) |
| 2807 |
SpellingEnum = CreateSemanticSpellings(Spellings, SemanticToSyntacticMap); |
| 2808 |
if (Header) |
| 2809 |
OS << SpellingEnum; |
| 2810 |
|
| 2811 |
const auto &ParsedAttrSpellingItr = |
| 2812 |
find_if(AttrMap, [R](const std::pair<std::string, const Record *> &P) { |
| 2813 |
return &R == P.second; |
| 2814 |
}); |
| 2815 |
|
| 2816 |
// Emit CreateImplicit factory methods. |
| 2817 |
auto emitCreate = [&](bool Implicit, bool DelayedArgsOnly, bool emitFake) { |
| 2818 |
if (Header) |
| 2819 |
OS << " static "; |
| 2820 |
OS << R.getName() << "Attr *"; |
| 2821 |
if (!Header) |
| 2822 |
OS << R.getName() << "Attr::"; |
| 2823 |
OS << "Create"; |
| 2824 |
if (Implicit) |
| 2825 |
OS << "Implicit"; |
| 2826 |
if (DelayedArgsOnly) |
| 2827 |
OS << "WithDelayedArgs"; |
| 2828 |
OS << "("; |
| 2829 |
OS << "ASTContext &Ctx"; |
| 2830 |
if (!DelayedArgsOnly) { |
| 2831 |
for (auto const &ai : Args) { |
| 2832 |
if (ai->isFake() && !emitFake) |
| 2833 |
continue; |
| 2834 |
OS << ", "; |
| 2835 |
ai->writeCtorParameters(OS); |
| 2836 |
} |
| 2837 |
} else { |
| 2838 |
OS << ", "; |
| 2839 |
DelayedArgs->writeCtorParameters(OS); |
| 2840 |
} |
| 2841 |
OS << ", const AttributeCommonInfo &CommonInfo"; |
| 2842 |
OS << ")"; |
| 2843 |
if (Header) { |
| 2844 |
OS << ";\n"; |
| 2845 |
return; |
| 2846 |
} |
| 2847 |
|
| 2848 |
OS << " {\n"; |
| 2849 |
OS << " auto *A = new (Ctx) " << R.getName(); |
| 2850 |
OS << "Attr(Ctx, CommonInfo"; |
| 2851 |
|
| 2852 |
if (!DelayedArgsOnly) { |
| 2853 |
for (auto const &ai : Args) { |
| 2854 |
if (ai->isFake() && !emitFake) |
| 2855 |
continue; |
| 2856 |
OS << ", "; |
| 2857 |
ai->writeImplicitCtorArgs(OS); |
| 2858 |
} |
| 2859 |
} |
| 2860 |
OS << ");\n"; |
| 2861 |
if (Implicit) { |
| 2862 |
OS << " A->setImplicit(true);\n"; |
| 2863 |
} |
| 2864 |
if (Implicit || ElideSpelling) { |
| 2865 |
OS << " if (!A->isAttributeSpellingListCalculated() && " |
| 2866 |
"!A->getAttrName())\n"; |
| 2867 |
OS << " A->setAttributeSpellingListIndex(0);\n"; |
| 2868 |
} |
| 2869 |
if (DelayedArgsOnly) { |
| 2870 |
OS << " A->setDelayedArgs(Ctx, "; |
| 2871 |
DelayedArgs->writeImplicitCtorArgs(OS); |
| 2872 |
OS << ");\n"; |
| 2873 |
} |
| 2874 |
OS << " return A;\n}\n\n"; |
| 2875 |
}; |
| 2876 |
|
| 2877 |
auto emitCreateNoCI = [&](bool Implicit, bool DelayedArgsOnly, |
| 2878 |
bool emitFake) { |
| 2879 |
if (Header) |
| 2880 |
OS << " static "; |
| 2881 |
OS << R.getName() << "Attr *"; |
| 2882 |
if (!Header) |
| 2883 |
OS << R.getName() << "Attr::"; |
| 2884 |
OS << "Create"; |
| 2885 |
if (Implicit) |
| 2886 |
OS << "Implicit"; |
| 2887 |
if (DelayedArgsOnly) |
| 2888 |
OS << "WithDelayedArgs"; |
| 2889 |
OS << "("; |
| 2890 |
OS << "ASTContext &Ctx"; |
| 2891 |
if (!DelayedArgsOnly) { |
| 2892 |
for (auto const &ai : Args) { |
| 2893 |
if (ai->isFake() && !emitFake) |
| 2894 |
continue; |
| 2895 |
OS << ", "; |
| 2896 |
ai->writeCtorParameters(OS); |
| 2897 |
} |
| 2898 |
} else { |
| 2899 |
OS << ", "; |
| 2900 |
DelayedArgs->writeCtorParameters(OS); |
| 2901 |
} |
| 2902 |
OS << ", SourceRange Range"; |
| 2903 |
if (Header) |
| 2904 |
OS << " = {}"; |
| 2905 |
if (Spellings.size() > 1) { |
| 2906 |
OS << ", Spelling S"; |
| 2907 |
if (Header) |
| 2908 |
OS << " = " << SemanticToSyntacticMap[0]; |
| 2909 |
} |
| 2910 |
OS << ")"; |
| 2911 |
if (Header) { |
| 2912 |
OS << ";\n"; |
| 2913 |
return; |
| 2914 |
} |
| 2915 |
|
| 2916 |
OS << " {\n"; |
| 2917 |
OS << " AttributeCommonInfo I(Range, "; |
| 2918 |
|
| 2919 |
if (ParsedAttrSpellingItr != std::end(AttrMap)) |
| 2920 |
OS << "AT_" << ParsedAttrSpellingItr->first; |
| 2921 |
else |
| 2922 |
OS << "NoSemaHandlerAttribute"; |
| 2923 |
|
| 2924 |
if (Spellings.size() == 0) { |
| 2925 |
OS << ", AttributeCommonInfo::Form::Implicit()"; |
| 2926 |
} else if (Spellings.size() == 1) { |
| 2927 |
OS << ", "; |
| 2928 |
emitFormInitializer(OS, Spellings[0], "0"); |
| 2929 |
} else { |
| 2930 |
OS << ", [&]() {\n"; |
| 2931 |
OS << " switch (S) {\n"; |
| 2932 |
std::set<std::string> Uniques; |
| 2933 |
unsigned Idx = 0; |
| 2934 |
for (auto I = Spellings.begin(), E = Spellings.end(); I != E; |
| 2935 |
++I, ++Idx) { |
| 2936 |
const FlattenedSpelling &S = *I; |
| 2937 |
const auto &Name = SemanticToSyntacticMap[Idx]; |
| 2938 |
if (Uniques.insert(Name).second) { |
| 2939 |
OS << " case " << Name << ":\n"; |
| 2940 |
OS << " return AttributeCommonInfo::Form"; |
| 2941 |
emitFormInitializer(OS, S, Name); |
| 2942 |
OS << ";\n"; |
| 2943 |
} |
| 2944 |
} |
| 2945 |
OS << " default:\n"; |
| 2946 |
OS << " llvm_unreachable(\"Unknown attribute spelling!\");\n" |
| 2947 |
<< " return AttributeCommonInfo::Form"; |
| 2948 |
emitFormInitializer(OS, Spellings[0], "0"); |
| 2949 |
OS << ";\n" |
| 2950 |
<< " }\n" |
| 2951 |
<< " }()"; |
| 2952 |
} |
| 2953 |
|
| 2954 |
OS << ");\n"; |
| 2955 |
OS << " return Create"; |
| 2956 |
if (Implicit) |
| 2957 |
OS << "Implicit"; |
| 2958 |
if (DelayedArgsOnly) |
| 2959 |
OS << "WithDelayedArgs"; |
| 2960 |
OS << "(Ctx"; |
| 2961 |
if (!DelayedArgsOnly) { |
| 2962 |
for (auto const &ai : Args) { |
| 2963 |
if (ai->isFake() && !emitFake) |
| 2964 |
continue; |
| 2965 |
OS << ", "; |
| 2966 |
ai->writeImplicitCtorArgs(OS); |
| 2967 |
} |
| 2968 |
} else { |
| 2969 |
OS << ", "; |
| 2970 |
DelayedArgs->writeImplicitCtorArgs(OS); |
| 2971 |
} |
| 2972 |
OS << ", I);\n"; |
| 2973 |
OS << "}\n\n"; |
| 2974 |
}; |
| 2975 |
|
| 2976 |
auto emitCreates = [&](bool DelayedArgsOnly, bool emitFake) { |
| 2977 |
emitCreate(true, DelayedArgsOnly, emitFake); |
| 2978 |
emitCreate(false, DelayedArgsOnly, emitFake); |
| 2979 |
emitCreateNoCI(true, DelayedArgsOnly, emitFake); |
| 2980 |
emitCreateNoCI(false, DelayedArgsOnly, emitFake); |
| 2981 |
}; |
| 2982 |
|
| 2983 |
if (Header) |
| 2984 |
OS << " // Factory methods\n"; |
| 2985 |
|
| 2986 |
// Emit a CreateImplicit that takes all the arguments. |
| 2987 |
emitCreates(false, true); |
| 2988 |
|
| 2989 |
// Emit a CreateImplicit that takes all the non-fake arguments. |
| 2990 |
if (HasFakeArg) |
| 2991 |
emitCreates(false, false); |
| 2992 |
|
| 2993 |
// Emit a CreateWithDelayedArgs that takes only the dependent argument |
| 2994 |
// expressions. |
| 2995 |
if (DelayedArgs) |
| 2996 |
emitCreates(true, false); |
| 2997 |
|
| 2998 |
// Emit constructors. |
| 2999 |
auto emitCtor = [&](bool emitOpt, bool emitFake, bool emitNoArgs) { |
| 3000 |
auto shouldEmitArg = [=](const std::unique_ptr<Argument> &arg) { |
| 3001 |
if (emitNoArgs) |
| 3002 |
return false; |
| 3003 |
if (arg->isFake()) |
| 3004 |
return emitFake; |
| 3005 |
if (arg->isOptional()) |
| 3006 |
return emitOpt; |
| 3007 |
return true; |
| 3008 |
}; |
| 3009 |
if (Header) |
| 3010 |
OS << " "; |
| 3011 |
else |
| 3012 |
OS << R.getName() << "Attr::"; |
| 3013 |
OS << R.getName() |
| 3014 |
<< "Attr(ASTContext &Ctx, const AttributeCommonInfo &CommonInfo"; |
| 3015 |
OS << '\n'; |
| 3016 |
for (auto const &ai : Args) { |
| 3017 |
if (!shouldEmitArg(ai)) |
| 3018 |
continue; |
| 3019 |
OS << " , "; |
| 3020 |
ai->writeCtorParameters(OS); |
| 3021 |
OS << "\n"; |
| 3022 |
} |
| 3023 |
|
| 3024 |
OS << " )"; |
| 3025 |
if (Header) { |
| 3026 |
OS << ";\n"; |
| 3027 |
return; |
| 3028 |
} |
| 3029 |
OS << "\n : " << SuperName << "(Ctx, CommonInfo, "; |
| 3030 |
OS << "attr::" << R.getName() << ", "; |
| 3031 |
|
| 3032 |
// Handle different late parsing modes. |
| 3033 |
OS << "/*IsLateParsed=*/"; |
| 3034 |
switch (getLateAttrParseKind(&R)) { |
| 3035 |
case LateAttrParseKind::Never: |
| 3036 |
OS << "false"; |
| 3037 |
break; |
| 3038 |
case LateAttrParseKind::ExperimentalExt: |
| 3039 |
// Currently no clients need to know the distinction between `Standard` |
| 3040 |
// and `ExperimentalExt` so treat `ExperimentalExt` just like |
| 3041 |
// `Standard` for now. |
| 3042 |
case LateAttrParseKind::Standard: |
| 3043 |
// Note: This is misleading. `IsLateParsed` doesn't mean the |
| 3044 |
// attribute was actually late parsed. Instead it means the attribute in |
| 3045 |
// `Attr.td` is marked as being late parsed. Maybe it should be called |
| 3046 |
// `IsLateParseable`? |
| 3047 |
OS << "true"; |
| 3048 |
break; |
| 3049 |
} |
| 3050 |
|
| 3051 |
if (Inheritable) { |
| 3052 |
OS << ", " |
| 3053 |
<< (R.getValueAsBit("InheritEvenIfAlreadyPresent") ? "true" |
| 3054 |
: "false"); |
| 3055 |
} |
| 3056 |
OS << ")\n"; |
| 3057 |
|
| 3058 |
for (auto const &ai : Args) { |
| 3059 |
OS << " , "; |
| 3060 |
if (!shouldEmitArg(ai)) { |
| 3061 |
ai->writeCtorDefaultInitializers(OS); |
| 3062 |
} else { |
| 3063 |
ai->writeCtorInitializers(OS); |
| 3064 |
} |
| 3065 |
OS << "\n"; |
| 3066 |
} |
| 3067 |
if (DelayedArgs) { |
| 3068 |
OS << " , "; |
| 3069 |
DelayedArgs->writeCtorDefaultInitializers(OS); |
| 3070 |
OS << "\n"; |
| 3071 |
} |
| 3072 |
|
| 3073 |
OS << " {\n"; |
| 3074 |
|
| 3075 |
for (auto const &ai : Args) { |
| 3076 |
if (!shouldEmitArg(ai)) |
| 3077 |
continue; |
| 3078 |
ai->writeCtorBody(OS); |
| 3079 |
} |
| 3080 |
OS << "}\n\n"; |
| 3081 |
}; |
| 3082 |
|
| 3083 |
if (Header) |
| 3084 |
OS << "\n // Constructors\n"; |
| 3085 |
|
| 3086 |
// Emit a constructor that includes all the arguments. |
| 3087 |
// This is necessary for cloning. |
| 3088 |
emitCtor(true, true, false); |
| 3089 |
|
| 3090 |
// Emit a constructor that takes all the non-fake arguments. |
| 3091 |
if (HasFakeArg) |
| 3092 |
emitCtor(true, false, false); |
| 3093 |
|
| 3094 |
// Emit a constructor that takes all the non-fake, non-optional arguments. |
| 3095 |
if (HasOptArg) |
| 3096 |
emitCtor(false, false, false); |
| 3097 |
|
| 3098 |
// Emit constructors that takes no arguments if none already exists. |
| 3099 |
// This is used for delaying arguments. |
| 3100 |
bool HasRequiredArgs = |
| 3101 |
count_if(Args, [=](const std::unique_ptr<Argument> &arg) { |
| 3102 |
return !arg->isFake() && !arg->isOptional(); |
| 3103 |
}); |
| 3104 |
if (DelayedArgs && HasRequiredArgs) |
| 3105 |
emitCtor(false, false, true); |
| 3106 |
|
| 3107 |
if (Header) { |
| 3108 |
OS << '\n'; |
| 3109 |
OS << " " << R.getName() << "Attr *clone(ASTContext &C) const;\n"; |
| 3110 |
OS << " void printPretty(raw_ostream &OS,\n" |
| 3111 |
<< " const PrintingPolicy &Policy) const;\n"; |
| 3112 |
OS << " const char *getSpelling() const;\n"; |
| 3113 |
} |
| 3114 |
|
| 3115 |
if (!ElideSpelling) { |
| 3116 |
assert(!SemanticToSyntacticMap.empty() && "Empty semantic mapping list"); |
| 3117 |
if (Header) |
| 3118 |
OS << " Spelling getSemanticSpelling() const;\n"; |
| 3119 |
else { |
| 3120 |
OS << R.getName() << "Attr::Spelling " << R.getName() |
| 3121 |
<< "Attr::getSemanticSpelling() const {\n"; |
| 3122 |
WriteSemanticSpellingSwitch("getAttributeSpellingListIndex()", |
| 3123 |
SemanticToSyntacticMap, OS); |
| 3124 |
OS << "}\n"; |
| 3125 |
} |
| 3126 |
} |
| 3127 |
|
| 3128 |
if (Header) |
| 3129 |
writeAttrAccessorDefinition(R, OS); |
| 3130 |
|
| 3131 |
for (auto const &ai : Args) { |
| 3132 |
if (Header) { |
| 3133 |
ai->writeAccessors(OS); |
| 3134 |
} else { |
| 3135 |
ai->writeAccessorDefinitions(OS); |
| 3136 |
} |
| 3137 |
OS << "\n\n"; |
| 3138 |
|
| 3139 |
// Don't write conversion routines for fake arguments. |
| 3140 |
if (ai->isFake()) continue; |
| 3141 |
|
| 3142 |
if (ai->isEnumArg()) |
| 3143 |
static_cast<const EnumArgument *>(ai.get())->writeConversion(OS, |
| 3144 |
Header); |
| 3145 |
else if (ai->isVariadicEnumArg()) |
| 3146 |
static_cast<const VariadicEnumArgument *>(ai.get())->writeConversion( |
| 3147 |
OS, Header); |
| 3148 |
} |
| 3149 |
|
| 3150 |
if (Header) { |
| 3151 |
if (DelayedArgs) { |
| 3152 |
DelayedArgs->writeAccessors(OS); |
| 3153 |
DelayedArgs->writeSetter(OS); |
| 3154 |
} |
| 3155 |
|
| 3156 |
OS << R.getValueAsString("AdditionalMembers"); |
| 3157 |
OS << "\n\n"; |
| 3158 |
|
| 3159 |
OS << " static bool classof(const Attr *A) { return A->getKind() == " |
| 3160 |
<< "attr::" << R.getName() << "; }\n"; |
| 3161 |
|
| 3162 |
OS << "};\n\n"; |
| 3163 |
} else { |
| 3164 |
if (DelayedArgs) |
| 3165 |
DelayedArgs->writeAccessorDefinitions(OS); |
| 3166 |
|
| 3167 |
OS << R.getName() << "Attr *" << R.getName() |
| 3168 |
<< "Attr::clone(ASTContext &C) const {\n"; |
| 3169 |
OS << " auto *A = new (C) " << R.getName() << "Attr(C, *this"; |
| 3170 |
for (auto const &ai : Args) { |
| 3171 |
OS << ", "; |
| 3172 |
ai->writeCloneArgs(OS); |
| 3173 |
} |
| 3174 |
OS << ");\n"; |
| 3175 |
OS << " A->Inherited = Inherited;\n"; |
| 3176 |
OS << " A->IsPackExpansion = IsPackExpansion;\n"; |
| 3177 |
OS << " A->setImplicit(Implicit);\n"; |
| 3178 |
if (DelayedArgs) { |
| 3179 |
OS << " A->setDelayedArgs(C, "; |
| 3180 |
DelayedArgs->writeCloneArgs(OS); |
| 3181 |
OS << ");\n"; |
| 3182 |
} |
| 3183 |
OS << " return A;\n}\n\n"; |
| 3184 |
|
| 3185 |
writePrettyPrintFunction(R, Args, OS); |
| 3186 |
writeGetSpellingFunction(R, OS); |
| 3187 |
} |
| 3188 |
} |
| 3189 |
} |
| 3190 |
// Emits the class definitions for attributes. |
| 3191 |
void clang::EmitClangAttrClass(const RecordKeeper &Records, raw_ostream &OS) { |
| 3192 |
emitSourceFileHeader("Attribute classes' definitions", OS, Records); |
| 3193 |
|
| 3194 |
OS << "#ifndef LLVM_CLANG_ATTR_CLASSES_INC\n"; |
| 3195 |
OS << "#define LLVM_CLANG_ATTR_CLASSES_INC\n"; |
| 3196 |
|
| 3197 |
emitAttributes(Records, OS, true); |
| 3198 |
|
| 3199 |
OS << "#endif // LLVM_CLANG_ATTR_CLASSES_INC\n"; |
| 3200 |
} |
| 3201 |
|
| 3202 |
// Emits the class method definitions for attributes. |
| 3203 |
void clang::EmitClangAttrImpl(const RecordKeeper &Records, raw_ostream &OS) { |
| 3204 |
emitSourceFileHeader("Attribute classes' member function definitions", OS, |
| 3205 |
Records); |
| 3206 |
|
| 3207 |
emitAttributes(Records, OS, false); |
| 3208 |
|
| 3209 |
// Instead of relying on virtual dispatch we just create a huge dispatch |
| 3210 |
// switch. This is both smaller and faster than virtual functions. |
| 3211 |
auto EmitFunc = [&](const char *Method) { |
| 3212 |
OS << " switch (getKind()) {\n"; |
| 3213 |
for (const auto *Attr : Records.getAllDerivedDefinitions("Attr")) { |
| 3214 |
const Record &R = *Attr; |
| 3215 |
if (!R.getValueAsBit("ASTNode")) |
| 3216 |
continue; |
| 3217 |
|
| 3218 |
OS << " case attr::" << R.getName() << ":\n"; |
| 3219 |
OS << " return cast<" << R.getName() << "Attr>(this)->" << Method |
| 3220 |
<< ";\n"; |
| 3221 |
} |
| 3222 |
OS << " }\n"; |
| 3223 |
OS << " llvm_unreachable(\"Unexpected attribute kind!\");\n"; |
| 3224 |
OS << "}\n\n"; |
| 3225 |
}; |
| 3226 |
|
| 3227 |
OS << "const char *Attr::getSpelling() const {\n"; |
| 3228 |
EmitFunc("getSpelling()"); |
| 3229 |
|
| 3230 |
OS << "Attr *Attr::clone(ASTContext &C) const {\n"; |
| 3231 |
EmitFunc("clone(C)"); |
| 3232 |
|
| 3233 |
OS << "void Attr::printPretty(raw_ostream &OS, " |
| 3234 |
"const PrintingPolicy &Policy) const {\n"; |
| 3235 |
EmitFunc("printPretty(OS, Policy)"); |
| 3236 |
} |
| 3237 |
|
| 3238 |
static void emitAttrList(raw_ostream &OS, StringRef Class, |
| 3239 |
ArrayRef<const Record *> AttrList) { |
| 3240 |
for (auto Cur : AttrList) { |
| 3241 |
OS << Class << "(" << Cur->getName() << ")\n"; |
| 3242 |
} |
| 3243 |
} |
| 3244 |
|
| 3245 |
// Determines if an attribute has a Pragma spelling. |
| 3246 |
static bool AttrHasPragmaSpelling(const Record *R) { |
| 3247 |
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R); |
| 3248 |
return any_of(Spellings, [](const FlattenedSpelling &S) { |
| 3249 |
return S.variety() == "Pragma"; |
| 3250 |
}); |
| 3251 |
} |
| 3252 |
|
| 3253 |
namespace { |
| 3254 |
|
| 3255 |
struct AttrClassDescriptor { |
| 3256 |
const char * const MacroName; |
| 3257 |
const char * const TableGenName; |
| 3258 |
}; |
| 3259 |
|
| 3260 |
} // end anonymous namespace |
| 3261 |
|
| 3262 |
static const AttrClassDescriptor AttrClassDescriptors[] = { |
| 3263 |
{"ATTR", "Attr"}, |
| 3264 |
{"TYPE_ATTR", "TypeAttr"}, |
| 3265 |
{"STMT_ATTR", "StmtAttr"}, |
| 3266 |
{"DECL_OR_STMT_ATTR", "DeclOrStmtAttr"}, |
| 3267 |
{"INHERITABLE_ATTR", "InheritableAttr"}, |
| 3268 |
{"DECL_OR_TYPE_ATTR", "DeclOrTypeAttr"}, |
| 3269 |
{"INHERITABLE_PARAM_ATTR", "InheritableParamAttr"}, |
| 3270 |
{"INHERITABLE_PARAM_OR_STMT_ATTR", "InheritableParamOrStmtAttr"}, |
| 3271 |
{"PARAMETER_ABI_ATTR", "ParameterABIAttr"}, |
| 3272 |
{"HLSL_ANNOTATION_ATTR", "HLSLAnnotationAttr"}, |
| 3273 |
{"HLSL_SEMANTIC_ATTR", "HLSLSemanticBaseAttr"}}; |
| 3274 |
|
| 3275 |
static void emitDefaultDefine(raw_ostream &OS, StringRef name, |
| 3276 |
const char *superName) { |
| 3277 |
OS << "#ifndef " << name << "\n"; |
| 3278 |
OS << "#define " << name << "(NAME) "; |
| 3279 |
if (superName) OS << superName << "(NAME)"; |
| 3280 |
OS << "\n#endif\n\n"; |
| 3281 |
} |
| 3282 |
|
| 3283 |
namespace { |
| 3284 |
|
| 3285 |
/// A class of attributes. |
| 3286 |
struct AttrClass { |
| 3287 |
const AttrClassDescriptor &Descriptor; |
| 3288 |
const Record *TheRecord; |
| 3289 |
AttrClass *SuperClass = nullptr; |
| 3290 |
std::vector<AttrClass*> SubClasses; |
| 3291 |
std::vector<const Record *> Attrs; |
| 3292 |
|
| 3293 |
AttrClass(const AttrClassDescriptor &Descriptor, const Record *R) |
| 3294 |
: Descriptor(Descriptor), TheRecord(R) {} |
| 3295 |
|
| 3296 |
void emitDefaultDefines(raw_ostream &OS) const { |
| 3297 |
// Default the macro unless this is a root class (i.e. Attr). |
| 3298 |
if (SuperClass) { |
| 3299 |
emitDefaultDefine(OS, Descriptor.MacroName, |
| 3300 |
SuperClass->Descriptor.MacroName); |
| 3301 |
} |
| 3302 |
} |
| 3303 |
|
| 3304 |
void emitUndefs(raw_ostream &OS) const { |
| 3305 |
OS << "#undef " << Descriptor.MacroName << "\n"; |
| 3306 |
} |
| 3307 |
|
| 3308 |
void emitAttrList(raw_ostream &OS) const { |
| 3309 |
for (auto SubClass : SubClasses) { |
| 3310 |
SubClass->emitAttrList(OS); |
| 3311 |
} |
| 3312 |
|
| 3313 |
::emitAttrList(OS, Descriptor.MacroName, Attrs); |
| 3314 |
} |
| 3315 |
|
| 3316 |
void classifyAttrOnRoot(const Record *Attr) { |
| 3317 |
bool result = classifyAttr(Attr); |
| 3318 |
assert(result && "failed to classify on root"); (void) result; |
| 3319 |
} |
| 3320 |
|
| 3321 |
void emitAttrRange(raw_ostream &OS) const { |
| 3322 |
OS << "ATTR_RANGE(" << Descriptor.TableGenName |
| 3323 |
<< ", " << getFirstAttr()->getName() |
| 3324 |
<< ", " << getLastAttr()->getName() << ")\n"; |
| 3325 |
} |
| 3326 |
|
| 3327 |
private: |
| 3328 |
bool classifyAttr(const Record *Attr) { |
| 3329 |
// Check all the subclasses. |
| 3330 |
for (auto SubClass : SubClasses) { |
| 3331 |
if (SubClass->classifyAttr(Attr)) |
| 3332 |
return true; |
| 3333 |
} |
| 3334 |
|
| 3335 |
// It's not more specific than this class, but it might still belong here. |
| 3336 |
if (Attr->isSubClassOf(TheRecord)) { |
| 3337 |
Attrs.push_back(Attr); |
| 3338 |
return true; |
| 3339 |
} |
| 3340 |
|
| 3341 |
return false; |
| 3342 |
} |
| 3343 |
|
| 3344 |
const Record *getFirstAttr() const { |
| 3345 |
if (!SubClasses.empty()) |
| 3346 |
return SubClasses.front()->getFirstAttr(); |
| 3347 |
return Attrs.front(); |
| 3348 |
} |
| 3349 |
|
| 3350 |
const Record *getLastAttr() const { |
| 3351 |
if (!Attrs.empty()) |
| 3352 |
return Attrs.back(); |
| 3353 |
return SubClasses.back()->getLastAttr(); |
| 3354 |
} |
| 3355 |
}; |
| 3356 |
|
| 3357 |
/// The entire hierarchy of attribute classes. |
| 3358 |
class AttrClassHierarchy { |
| 3359 |
std::vector<std::unique_ptr<AttrClass>> Classes; |
| 3360 |
|
| 3361 |
public: |
| 3362 |
AttrClassHierarchy(const RecordKeeper &Records) { |
| 3363 |
// Find records for all the classes. |
| 3364 |
for (auto &Descriptor : AttrClassDescriptors) { |
| 3365 |
const Record *ClassRecord = Records.getClass(Descriptor.TableGenName); |
| 3366 |
AttrClass *Class = new AttrClass(Descriptor, ClassRecord); |
| 3367 |
Classes.emplace_back(Class); |
| 3368 |
} |
| 3369 |
|
| 3370 |
// Link up the hierarchy. |
| 3371 |
for (auto &Class : Classes) { |
| 3372 |
if (AttrClass *SuperClass = findSuperClass(Class->TheRecord)) { |
| 3373 |
Class->SuperClass = SuperClass; |
| 3374 |
SuperClass->SubClasses.push_back(Class.get()); |
| 3375 |
} |
| 3376 |
} |
| 3377 |
|
| 3378 |
#ifndef NDEBUG |
| 3379 |
for (auto i = Classes.begin(), e = Classes.end(); i != e; ++i) { |
| 3380 |
assert((i == Classes.begin()) == ((*i)->SuperClass == nullptr) && |
| 3381 |
"only the first class should be a root class!"); |
| 3382 |
} |
| 3383 |
#endif |
| 3384 |
} |
| 3385 |
|
| 3386 |
void emitDefaultDefines(raw_ostream &OS) const { |
| 3387 |
for (auto &Class : Classes) { |
| 3388 |
Class->emitDefaultDefines(OS); |
| 3389 |
} |
| 3390 |
} |
| 3391 |
|
| 3392 |
void emitUndefs(raw_ostream &OS) const { |
| 3393 |
for (auto &Class : Classes) { |
| 3394 |
Class->emitUndefs(OS); |
| 3395 |
} |
| 3396 |
} |
| 3397 |
|
| 3398 |
void emitAttrLists(raw_ostream &OS) const { |
| 3399 |
// Just start from the root class. |
| 3400 |
Classes[0]->emitAttrList(OS); |
| 3401 |
} |
| 3402 |
|
| 3403 |
void emitAttrRanges(raw_ostream &OS) const { |
| 3404 |
for (auto &Class : Classes) |
| 3405 |
Class->emitAttrRange(OS); |
| 3406 |
} |
| 3407 |
|
| 3408 |
void classifyAttr(const Record *Attr) { |
| 3409 |
// Add the attribute to the root class. |
| 3410 |
Classes[0]->classifyAttrOnRoot(Attr); |
| 3411 |
} |
| 3412 |
|
| 3413 |
private: |
| 3414 |
AttrClass *findClassByRecord(const Record *R) const { |
| 3415 |
for (auto &Class : Classes) { |
| 3416 |
if (Class->TheRecord == R) |
| 3417 |
return Class.get(); |
| 3418 |
} |
| 3419 |
return nullptr; |
| 3420 |
} |
| 3421 |
|
| 3422 |
AttrClass *findSuperClass(const Record *R) const { |
| 3423 |
// TableGen flattens the superclass list, so we just need to walk it |
| 3424 |
// in reverse. |
| 3425 |
std::vector<const Record *> SuperClasses = R->getSuperClasses(); |
| 3426 |
for (const Record *R : reverse(SuperClasses)) { |
| 3427 |
if (AttrClass *SuperClass = findClassByRecord(R)) |
| 3428 |
return SuperClass; |
| 3429 |
} |
| 3430 |
return nullptr; |
| 3431 |
} |
| 3432 |
}; |
| 3433 |
|
| 3434 |
} // end anonymous namespace |
| 3435 |
|
| 3436 |
namespace clang { |
| 3437 |
|
| 3438 |
// Emits the enumeration list for attributes. |
| 3439 |
void EmitClangAttrList(const RecordKeeper &Records, raw_ostream &OS) { |
| 3440 |
emitSourceFileHeader("List of all attributes that Clang recognizes", OS, |
| 3441 |
Records); |
| 3442 |
|
| 3443 |
AttrClassHierarchy Hierarchy(Records); |
| 3444 |
|
| 3445 |
// Add defaulting macro definitions. |
| 3446 |
Hierarchy.emitDefaultDefines(OS); |
| 3447 |
emitDefaultDefine(OS, "PRAGMA_SPELLING_ATTR", nullptr); |
| 3448 |
|
| 3449 |
std::vector<const Record *> PragmaAttrs; |
| 3450 |
for (auto *Attr : Records.getAllDerivedDefinitions("Attr")) { |
| 3451 |
if (!Attr->getValueAsBit("ASTNode")) |
| 3452 |
continue; |
| 3453 |
|
| 3454 |
// Add the attribute to the ad-hoc groups. |
| 3455 |
if (AttrHasPragmaSpelling(Attr)) |
| 3456 |
PragmaAttrs.push_back(Attr); |
| 3457 |
|
| 3458 |
// Place it in the hierarchy. |
| 3459 |
Hierarchy.classifyAttr(Attr); |
| 3460 |
} |
| 3461 |
|
| 3462 |
// Emit the main attribute list. |
| 3463 |
Hierarchy.emitAttrLists(OS); |
| 3464 |
|
| 3465 |
// Emit the ad hoc groups. |
| 3466 |
emitAttrList(OS, "PRAGMA_SPELLING_ATTR", PragmaAttrs); |
| 3467 |
|
| 3468 |
// Emit the attribute ranges. |
| 3469 |
OS << "#ifdef ATTR_RANGE\n"; |
| 3470 |
Hierarchy.emitAttrRanges(OS); |
| 3471 |
OS << "#undef ATTR_RANGE\n"; |
| 3472 |
OS << "#endif\n"; |
| 3473 |
|
| 3474 |
Hierarchy.emitUndefs(OS); |
| 3475 |
OS << "#undef PRAGMA_SPELLING_ATTR\n"; |
| 3476 |
} |
| 3477 |
|
| 3478 |
// Emits the enumeration list for attributes. |
| 3479 |
void EmitClangAttrSubjectMatchRuleList(const RecordKeeper &Records, |
| 3480 |
raw_ostream &OS) { |
| 3481 |
emitSourceFileHeader( |
| 3482 |
"List of all attribute subject matching rules that Clang recognizes", OS, |
| 3483 |
Records); |
| 3484 |
PragmaClangAttributeSupport &PragmaAttributeSupport = |
| 3485 |
getPragmaAttributeSupport(Records); |
| 3486 |
emitDefaultDefine(OS, "ATTR_MATCH_RULE", nullptr); |
| 3487 |
PragmaAttributeSupport.emitMatchRuleList(OS); |
| 3488 |
OS << "#undef ATTR_MATCH_RULE\n"; |
| 3489 |
} |
| 3490 |
|
| 3491 |
// Emits the code to read an attribute from a precompiled header. |
| 3492 |
void EmitClangAttrPCHRead(const RecordKeeper &Records, raw_ostream &OS) { |
| 3493 |
emitSourceFileHeader("Attribute deserialization code", OS, Records); |
| 3494 |
|
| 3495 |
const Record *InhClass = Records.getClass("InheritableAttr"); |
| 3496 |
std::vector<const Record *> ArgRecords; |
| 3497 |
std::vector<std::unique_ptr<Argument>> Args; |
| 3498 |
std::unique_ptr<VariadicExprArgument> DelayedArgs; |
| 3499 |
|
| 3500 |
OS << " switch (Kind) {\n"; |
| 3501 |
for (const auto *Attr : Records.getAllDerivedDefinitions("Attr")) { |
| 3502 |
const Record &R = *Attr; |
| 3503 |
if (!R.getValueAsBit("ASTNode")) |
| 3504 |
continue; |
| 3505 |
|
| 3506 |
OS << " case attr::" << R.getName() << ": {\n"; |
| 3507 |
if (R.isSubClassOf(InhClass)) |
| 3508 |
OS << " bool isInherited = Record.readInt();\n"; |
| 3509 |
OS << " bool isImplicit = Record.readInt();\n"; |
| 3510 |
OS << " bool isPackExpansion = Record.readInt();\n"; |
| 3511 |
DelayedArgs = nullptr; |
| 3512 |
if (Attr->getValueAsBit("AcceptsExprPack")) { |
| 3513 |
DelayedArgs = |
| 3514 |
std::make_unique<VariadicExprArgument>("DelayedArgs", R.getName()); |
| 3515 |
DelayedArgs->writePCHReadDecls(OS); |
| 3516 |
} |
| 3517 |
ArgRecords = R.getValueAsListOfDefs("Args"); |
| 3518 |
Args.clear(); |
| 3519 |
for (const auto *Arg : ArgRecords) { |
| 3520 |
Args.emplace_back(createArgument(*Arg, R.getName())); |
| 3521 |
Args.back()->writePCHReadDecls(OS); |
| 3522 |
} |
| 3523 |
OS << " New = new (Context) " << R.getName() << "Attr(Context, Info"; |
| 3524 |
for (auto const &ri : Args) { |
| 3525 |
OS << ", "; |
| 3526 |
ri->writePCHReadArgs(OS); |
| 3527 |
} |
| 3528 |
OS << ");\n"; |
| 3529 |
if (R.isSubClassOf(InhClass)) |
| 3530 |
OS << " cast<InheritableAttr>(New)->setInherited(isInherited);\n"; |
| 3531 |
OS << " New->setImplicit(isImplicit);\n"; |
| 3532 |
OS << " New->setPackExpansion(isPackExpansion);\n"; |
| 3533 |
if (DelayedArgs) { |
| 3534 |
OS << " cast<" << R.getName() |
| 3535 |
<< "Attr>(New)->setDelayedArgs(Context, "; |
| 3536 |
DelayedArgs->writePCHReadArgs(OS); |
| 3537 |
OS << ");\n"; |
| 3538 |
} |
| 3539 |
|
| 3540 |
if (Attr->getValueAsBit("HasCustomSerialization")) |
| 3541 |
OS << " read" << R.getName() << "Attr(cast<" << R.getName() |
| 3542 |
<< "Attr>(New));\n"; |
| 3543 |
|
| 3544 |
OS << " break;\n"; |
| 3545 |
OS << " }\n"; |
| 3546 |
} |
| 3547 |
OS << " }\n"; |
| 3548 |
} |
| 3549 |
|
| 3550 |
// Emits the code to write an attribute to a precompiled header. |
| 3551 |
void EmitClangAttrPCHWrite(const RecordKeeper &Records, raw_ostream &OS) { |
| 3552 |
emitSourceFileHeader("Attribute serialization code", OS, Records); |
| 3553 |
|
| 3554 |
const Record *InhClass = Records.getClass("InheritableAttr"); |
| 3555 |
OS << " switch (A->getKind()) {\n"; |
| 3556 |
for (const auto *Attr : Records.getAllDerivedDefinitions("Attr")) { |
| 3557 |
const Record &R = *Attr; |
| 3558 |
if (!R.getValueAsBit("ASTNode")) |
| 3559 |
continue; |
| 3560 |
OS << " case attr::" << R.getName() << ": {\n"; |
| 3561 |
std::vector<const Record *> Args = R.getValueAsListOfDefs("Args"); |
| 3562 |
if (R.isSubClassOf(InhClass) || !Args.empty()) |
| 3563 |
OS << " const auto *SA = cast<" << R.getName() |
| 3564 |
<< "Attr>(A);\n"; |
| 3565 |
if (R.isSubClassOf(InhClass)) |
| 3566 |
OS << " Record.push_back(SA->isInherited());\n"; |
| 3567 |
OS << " Record.push_back(A->isImplicit());\n"; |
| 3568 |
OS << " Record.push_back(A->isPackExpansion());\n"; |
| 3569 |
if (Attr->getValueAsBit("AcceptsExprPack")) |
| 3570 |
VariadicExprArgument("DelayedArgs", R.getName()).writePCHWrite(OS); |
| 3571 |
|
| 3572 |
for (const auto *Arg : Args) |
| 3573 |
createArgument(*Arg, R.getName())->writePCHWrite(OS); |
| 3574 |
|
| 3575 |
if (Attr->getValueAsBit("HasCustomSerialization")) |
| 3576 |
OS << " Record.Add" << R.getName() << "Attr(SA);\n"; |
| 3577 |
|
| 3578 |
OS << " break;\n"; |
| 3579 |
OS << " }\n"; |
| 3580 |
} |
| 3581 |
OS << " }\n"; |
| 3582 |
} |
| 3583 |
|
| 3584 |
} // namespace clang |
| 3585 |
|
| 3586 |
// Helper function for GenerateTargetSpecificAttrChecks that alters the 'Test' |
| 3587 |
// parameter with only a single check type, if applicable. |
| 3588 |
static bool GenerateTargetSpecificAttrCheck(const Record *R, std::string &Test, |
| 3589 |
std::string *FnName, |
| 3590 |
StringRef ListName, |
| 3591 |
StringRef CheckAgainst, |
| 3592 |
StringRef Scope) { |
| 3593 |
if (!R->isValueUnset(ListName)) { |
| 3594 |
Test += " && ("; |
| 3595 |
std::vector<StringRef> Items = R->getValueAsListOfStrings(ListName); |
| 3596 |
for (auto I = Items.begin(), E = Items.end(); I != E; ++I) { |
| 3597 |
StringRef Part = *I; |
| 3598 |
Test += CheckAgainst; |
| 3599 |
Test += " == "; |
| 3600 |
Test += Scope; |
| 3601 |
Test += Part; |
| 3602 |
if (I + 1 != E) |
| 3603 |
Test += " || "; |
| 3604 |
if (FnName) |
| 3605 |
*FnName += Part; |
| 3606 |
} |
| 3607 |
Test += ")"; |
| 3608 |
return true; |
| 3609 |
} |
| 3610 |
return false; |
| 3611 |
} |
| 3612 |
|
| 3613 |
// Generate a conditional expression to check if the current target satisfies |
| 3614 |
// the conditions for a TargetSpecificAttr record, and append the code for |
| 3615 |
// those checks to the Test string. If the FnName string pointer is non-null, |
| 3616 |
// append a unique suffix to distinguish this set of target checks from other |
| 3617 |
// TargetSpecificAttr records. |
| 3618 |
static bool GenerateTargetSpecificAttrChecks(const Record *R, |
| 3619 |
std::vector<StringRef> &Arches, |
| 3620 |
std::string &Test, |
| 3621 |
std::string *FnName) { |
| 3622 |
bool AnyTargetChecks = false; |
| 3623 |
|
| 3624 |
// It is assumed that there will be an Triple object |
| 3625 |
// named "T" and a TargetInfo object named "Target" within |
| 3626 |
// scope that can be used to determine whether the attribute exists in |
| 3627 |
// a given target. |
| 3628 |
Test += "true"; |
| 3629 |
// If one or more architectures is specified, check those. Arches are handled |
| 3630 |
// differently because GenerateTargetRequirements needs to combine the list |
| 3631 |
// with ParseKind. |
| 3632 |
if (!Arches.empty()) { |
| 3633 |
AnyTargetChecks = true; |
| 3634 |
Test += " && ("; |
| 3635 |
for (auto I = Arches.begin(), E = Arches.end(); I != E; ++I) { |
| 3636 |
StringRef Part = *I; |
| 3637 |
Test += "T.getArch() == llvm::Triple::"; |
| 3638 |
Test += Part; |
| 3639 |
if (I + 1 != E) |
| 3640 |
Test += " || "; |
| 3641 |
if (FnName) |
| 3642 |
*FnName += Part; |
| 3643 |
} |
| 3644 |
Test += ")"; |
| 3645 |
} |
| 3646 |
|
| 3647 |
// If the attribute is specific to particular OSes, check those. |
| 3648 |
AnyTargetChecks |= GenerateTargetSpecificAttrCheck( |
| 3649 |
R, Test, FnName, "OSes", "T.getOS()", "llvm::Triple::"); |
| 3650 |
|
| 3651 |
// If one or more object formats is specified, check those. |
| 3652 |
AnyTargetChecks |= |
| 3653 |
GenerateTargetSpecificAttrCheck(R, Test, FnName, "ObjectFormats", |
| 3654 |
"T.getObjectFormat()", "llvm::Triple::"); |
| 3655 |
|
| 3656 |
// If custom code is specified, emit it. |
| 3657 |
StringRef Code = R->getValueAsString("CustomCode"); |
| 3658 |
if (!Code.empty()) { |
| 3659 |
AnyTargetChecks = true; |
| 3660 |
Test += " && ("; |
| 3661 |
Test += Code; |
| 3662 |
Test += ")"; |
| 3663 |
} |
| 3664 |
|
| 3665 |
return AnyTargetChecks; |
| 3666 |
} |
| 3667 |
|
| 3668 |
static void GenerateHasAttrSpellingStringSwitch( |
| 3669 |
ArrayRef<std::pair<const Record *, FlattenedSpelling>> Attrs, |
| 3670 |
raw_ostream &OS, StringRef Variety, StringRef Scope = "") { |
| 3671 |
|
| 3672 |
// It turns out that there are duplicate records for a given spelling. This |
| 3673 |
// map combines matching test strings using '||'. For example, if there are |
| 3674 |
// three conditions A, B, and C, the final result will be: A || B || C. |
| 3675 |
llvm::StringMap<std::string> TestStringMap; |
| 3676 |
|
| 3677 |
for (const auto &[Attr, Spelling] : Attrs) { |
| 3678 |
// C++11-style attributes have specific version information associated with |
| 3679 |
// them. If the attribute has no scope, the version information must not |
| 3680 |
// have the default value (1), as that's incorrect. Instead, the unscoped |
| 3681 |
// attribute version information should be taken from the SD-6 standing |
| 3682 |
// document, which can be found at: |
| 3683 |
// https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations |
| 3684 |
// |
| 3685 |
// C23-style attributes have the same kind of version information |
| 3686 |
// associated with them. The unscoped attribute version information should |
| 3687 |
// be taken from the specification of the attribute in the C Standard. |
| 3688 |
// |
| 3689 |
// Clang-specific attributes have the same kind of version information |
| 3690 |
// associated with them. This version is typically the default value (1). |
| 3691 |
// These version values are clang-specific and should typically be |
| 3692 |
// incremented once the attribute changes its syntax and/or semantics in a |
| 3693 |
// a way that is impactful to the end user. |
| 3694 |
int Version = 1; |
| 3695 |
|
| 3696 |
assert(Spelling.variety() == Variety); |
| 3697 |
std::string Name = ""; |
| 3698 |
if (Spelling.nameSpace().empty() || Scope == Spelling.nameSpace()) { |
| 3699 |
Name = Spelling.name(); |
| 3700 |
Version = static_cast<int>( |
| 3701 |
Spelling.getSpellingRecord().getValueAsInt("Version")); |
| 3702 |
// Verify that explicitly specified CXX11 and C23 spellings (i.e. |
| 3703 |
// not inferred from Clang/GCC spellings) have a version that's |
| 3704 |
// different from the default (1). |
| 3705 |
bool RequiresValidVersion = |
| 3706 |
(Variety == "CXX11" || Variety == "C23") && |
| 3707 |
Spelling.getSpellingRecord().getValueAsString("Variety") == Variety; |
| 3708 |
if (RequiresValidVersion && Scope.empty() && Version == 1) |
| 3709 |
PrintError(Spelling.getSpellingRecord().getLoc(), |
| 3710 |
"Standard attributes must have " |
| 3711 |
"valid version information."); |
| 3712 |
} |
| 3713 |
|
| 3714 |
std::string Test; |
| 3715 |
if (Attr->isSubClassOf("TargetSpecificAttr")) { |
| 3716 |
const Record *R = Attr->getValueAsDef("Target"); |
| 3717 |
std::vector<StringRef> Arches = R->getValueAsListOfStrings("Arches"); |
| 3718 |
GenerateTargetSpecificAttrChecks(R, Arches, Test, nullptr); |
| 3719 |
} else if (!Attr->getValueAsListOfDefs("TargetSpecificSpellings").empty()) { |
| 3720 |
// Add target checks if this spelling is target-specific. |
| 3721 |
for (const auto &TargetSpelling : |
| 3722 |
Attr->getValueAsListOfDefs("TargetSpecificSpellings")) { |
| 3723 |
// Find spelling that matches current scope and name. |
| 3724 |
for (const auto &Spelling : GetFlattenedSpellings(*TargetSpelling)) { |
| 3725 |
if (Scope == Spelling.nameSpace() && Name == Spelling.name()) { |
| 3726 |
const Record *Target = TargetSpelling->getValueAsDef("Target"); |
| 3727 |
std::vector<StringRef> Arches = |
| 3728 |
Target->getValueAsListOfStrings("Arches"); |
| 3729 |
GenerateTargetSpecificAttrChecks(Target, Arches, Test, |
| 3730 |
/*FnName=*/nullptr); |
| 3731 |
break; |
| 3732 |
} |
| 3733 |
} |
| 3734 |
} |
| 3735 |
} |
| 3736 |
|
| 3737 |
std::string TestStr = |
| 3738 |
!Test.empty() ? '(' + Test + " ? " + itostr(Version) + " : 0" + ')' |
| 3739 |
: '(' + itostr(Version) + ')'; |
| 3740 |
|
| 3741 |
if (Scope.empty() || Scope == Spelling.nameSpace()) { |
| 3742 |
if (TestStringMap.contains(Spelling.name()) && |
| 3743 |
TestStringMap[Spelling.name()] != TestStr) |
| 3744 |
TestStringMap[Spelling.name()] += " || " + TestStr; |
| 3745 |
else |
| 3746 |
TestStringMap[Spelling.name()] = TestStr; |
| 3747 |
} |
| 3748 |
} |
| 3749 |
|
| 3750 |
// Create the actual string switch statement after all the attributes have |
| 3751 |
// been parsed. |
| 3752 |
for (auto &Entry : TestStringMap) { |
| 3753 |
OS << " .Case(\"" << Entry.getKey() << "\", " << Entry.getValue() |
| 3754 |
<< ")\n"; |
| 3755 |
} |
| 3756 |
|
| 3757 |
OS << " .Default(0);\n"; |
| 3758 |
} |
| 3759 |
|
| 3760 |
namespace clang { |
| 3761 |
|
| 3762 |
// Emits list of regular keyword attributes with info about their arguments. |
| 3763 |
void EmitClangRegularKeywordAttributeInfo(const RecordKeeper &Records, |
| 3764 |
raw_ostream &OS) { |
| 3765 |
emitSourceFileHeader( |
| 3766 |
"A list of regular keyword attributes generated from the attribute" |
| 3767 |
" definitions", |
| 3768 |
OS); |
| 3769 |
// Assume for now that the same token is not used in multiple regular |
| 3770 |
// keyword attributes. |
| 3771 |
for (auto *R : Records.getAllDerivedDefinitions("Attr")) |
| 3772 |
for (const auto &S : GetFlattenedSpellings(*R)) { |
| 3773 |
if (!isRegularKeywordAttribute(S)) |
| 3774 |
continue; |
| 3775 |
std::vector<const Record *> Args = R->getValueAsListOfDefs("Args"); |
| 3776 |
bool HasArgs = any_of( |
| 3777 |
Args, [](const Record *Arg) { return !Arg->getValueAsBit("Fake"); }); |
| 3778 |
|
| 3779 |
OS << "KEYWORD_ATTRIBUTE(" |
| 3780 |
<< S.getSpellingRecord().getValueAsString("Name") << ", " |
| 3781 |
<< (HasArgs ? "true" : "false") << ", )\n"; |
| 3782 |
} |
| 3783 |
OS << "#undef KEYWORD_ATTRIBUTE\n"; |
| 3784 |
} |
| 3785 |
|
| 3786 |
void EmitCXX11AttributeInfo(const RecordKeeper &Records, raw_ostream &OS) { |
| 3787 |
OS << "#if defined(CXX11_ATTR_ARGS_INFO)\n"; |
| 3788 |
for (auto *R : Records.getAllDerivedDefinitions("Attr")) { |
| 3789 |
for (const FlattenedSpelling &SI : GetFlattenedSpellings(*R)) { |
| 3790 |
if (SI.variety() == "CXX11" && SI.nameSpace().empty()) { |
| 3791 |
unsigned RequiredArgs = 0; |
| 3792 |
unsigned OptionalArgs = 0; |
| 3793 |
for (const auto *Arg : R->getValueAsListOfDefs("Args")) { |
| 3794 |
if (Arg->getValueAsBit("Fake")) |
| 3795 |
continue; |
| 3796 |
|
| 3797 |
if (Arg->getValueAsBit("Optional")) |
| 3798 |
OptionalArgs++; |
| 3799 |
else |
| 3800 |
RequiredArgs++; |
| 3801 |
} |
| 3802 |
OS << ".Case(\"" << SI.getSpellingRecord().getValueAsString("Name") |
| 3803 |
<< "\"," |
| 3804 |
<< "AttributeCommonInfo::AttrArgsInfo::" |
| 3805 |
<< (RequiredArgs ? "Required" |
| 3806 |
: OptionalArgs ? "Optional" |
| 3807 |
: "None") |
| 3808 |
<< ")" |
| 3809 |
<< "\n"; |
| 3810 |
} |
| 3811 |
} |
| 3812 |
} |
| 3813 |
OS << "#endif // CXX11_ATTR_ARGS_INFO\n"; |
| 3814 |
} |
| 3815 |
|
| 3816 |
// Emits the list of spellings for attributes. |
| 3817 |
void EmitClangAttrHasAttrImpl(const RecordKeeper &Records, raw_ostream &OS) { |
| 3818 |
emitSourceFileHeader("Code to implement the __has_attribute logic", OS, |
| 3819 |
Records); |
| 3820 |
|
| 3821 |
// Separate all of the attributes out into four group: generic, C++11, GNU, |
| 3822 |
// and declspecs. Then generate a big switch statement for each of them. |
| 3823 |
using PairTy = std::pair<const Record *, FlattenedSpelling>; |
| 3824 |
std::vector<PairTy> Declspec, Microsoft, GNU, Pragma, HLSLAnnotation; |
| 3825 |
std::map<StringRef, std::vector<PairTy>> CXX, C23; |
| 3826 |
|
| 3827 |
// Walk over the list of all attributes, and split them out based on the |
| 3828 |
// spelling variety. |
| 3829 |
for (auto *R : Records.getAllDerivedDefinitions("Attr")) { |
| 3830 |
for (const FlattenedSpelling &SI : GetFlattenedSpellings(*R)) { |
| 3831 |
StringRef Variety = SI.variety(); |
| 3832 |
if (Variety == "GNU") |
| 3833 |
GNU.emplace_back(R, SI); |
| 3834 |
else if (Variety == "Declspec") |
| 3835 |
Declspec.emplace_back(R, SI); |
| 3836 |
else if (Variety == "Microsoft") |
| 3837 |
Microsoft.emplace_back(R, SI); |
| 3838 |
else if (Variety == "CXX11") |
| 3839 |
CXX[SI.nameSpace()].emplace_back(R, SI); |
| 3840 |
else if (Variety == "C23") |
| 3841 |
C23[SI.nameSpace()].emplace_back(R, SI); |
| 3842 |
else if (Variety == "Pragma") |
| 3843 |
Pragma.emplace_back(R, SI); |
| 3844 |
else if (Variety == "HLSLAnnotation") |
| 3845 |
HLSLAnnotation.emplace_back(R, SI); |
| 3846 |
} |
| 3847 |
} |
| 3848 |
|
| 3849 |
OS << "const llvm::Triple &T = Target.getTriple();\n"; |
| 3850 |
OS << "switch (Syntax) {\n"; |
| 3851 |
OS << "case AttributeCommonInfo::Syntax::AS_GNU:\n"; |
| 3852 |
OS << " return llvm::StringSwitch<int>(Name)\n"; |
| 3853 |
GenerateHasAttrSpellingStringSwitch(GNU, OS, "GNU"); |
| 3854 |
OS << "case AttributeCommonInfo::Syntax::AS_Declspec:\n"; |
| 3855 |
OS << " return llvm::StringSwitch<int>(Name)\n"; |
| 3856 |
GenerateHasAttrSpellingStringSwitch(Declspec, OS, "Declspec"); |
| 3857 |
OS << "case AttributeCommonInfo::Syntax::AS_Microsoft:\n"; |
| 3858 |
OS << " return llvm::StringSwitch<int>(Name)\n"; |
| 3859 |
GenerateHasAttrSpellingStringSwitch(Microsoft, OS, "Microsoft"); |
| 3860 |
OS << "case AttributeCommonInfo::Syntax::AS_Pragma:\n"; |
| 3861 |
OS << " return llvm::StringSwitch<int>(Name)\n"; |
| 3862 |
GenerateHasAttrSpellingStringSwitch(Pragma, OS, "Pragma"); |
| 3863 |
OS << "case AttributeCommonInfo::Syntax::AS_HLSLAnnotation:\n"; |
| 3864 |
OS << " return llvm::StringSwitch<int>(Name)\n"; |
| 3865 |
GenerateHasAttrSpellingStringSwitch(HLSLAnnotation, OS, "HLSLAnnotation"); |
| 3866 |
auto fn = [&OS](StringRef Spelling, |
| 3867 |
const std::map<StringRef, std::vector<PairTy>> &Map) { |
| 3868 |
OS << "case AttributeCommonInfo::Syntax::AS_" << Spelling << ": {\n"; |
| 3869 |
// C++11-style attributes are further split out based on the Scope. |
| 3870 |
ListSeparator LS(" else "); |
| 3871 |
for (const auto &[Scope, List] : Map) { |
| 3872 |
OS << LS; |
| 3873 |
OS << "if (ScopeName == \"" << Scope << "\") {\n"; |
| 3874 |
OS << " return llvm::StringSwitch<int>(Name)\n"; |
| 3875 |
GenerateHasAttrSpellingStringSwitch(List, OS, Spelling, Scope); |
| 3876 |
OS << "}"; |
| 3877 |
} |
| 3878 |
OS << "\n} break;\n"; |
| 3879 |
}; |
| 3880 |
fn("CXX11", CXX); |
| 3881 |
fn("C23", C23); |
| 3882 |
OS << "case AttributeCommonInfo::Syntax::AS_Keyword:\n"; |
| 3883 |
OS << "case AttributeCommonInfo::Syntax::AS_ContextSensitiveKeyword:\n"; |
| 3884 |
OS << " llvm_unreachable(\"hasAttribute not supported for keyword\");\n"; |
| 3885 |
OS << " return 0;\n"; |
| 3886 |
OS << "case AttributeCommonInfo::Syntax::AS_Implicit:\n"; |
| 3887 |
OS << " llvm_unreachable (\"hasAttribute not supported for " |
| 3888 |
"AS_Implicit\");\n"; |
| 3889 |
OS << " return 0;\n"; |
| 3890 |
|
| 3891 |
OS << "}\n"; |
| 3892 |
} |
| 3893 |
|
| 3894 |
void EmitClangAttrSpellingListIndex(const RecordKeeper &Records, |
| 3895 |
raw_ostream &OS) { |
| 3896 |
emitSourceFileHeader("Code to translate different attribute spellings into " |
| 3897 |
"internal identifiers", |
| 3898 |
OS, Records); |
| 3899 |
|
| 3900 |
OS << " switch (getParsedKind()) {\n"; |
| 3901 |
OS << " case IgnoredAttribute:\n"; |
| 3902 |
OS << " case UnknownAttribute:\n"; |
| 3903 |
OS << " case NoSemaHandlerAttribute:\n"; |
| 3904 |
OS << " llvm_unreachable(\"Ignored/unknown shouldn't get here\");\n"; |
| 3905 |
|
| 3906 |
ParsedAttrMap Attrs = getParsedAttrList(Records); |
| 3907 |
for (const auto &I : Attrs) { |
| 3908 |
const Record &R = *I.second; |
| 3909 |
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R); |
| 3910 |
OS << " case AT_" << I.first << ": {\n"; |
| 3911 |
|
| 3912 |
// If there are none or one spelling to check, resort to the default |
| 3913 |
// behavior of returning index as 0. |
| 3914 |
if (Spellings.size() <= 1) { |
| 3915 |
OS << " return 0;\n" |
| 3916 |
<< " break;\n" |
| 3917 |
<< " }\n"; |
| 3918 |
continue; |
| 3919 |
} |
| 3920 |
|
| 3921 |
std::vector<StringRef> Names; |
| 3922 |
llvm::transform(Spellings, std::back_inserter(Names), |
| 3923 |
[](const FlattenedSpelling &FS) { return FS.name(); }); |
| 3924 |
llvm::sort(Names); |
| 3925 |
Names.erase(llvm::unique(Names), Names.end()); |
| 3926 |
|
| 3927 |
for (const auto &[Idx, FS] : enumerate(Spellings)) { |
| 3928 |
OS << " if ("; |
| 3929 |
if (Names.size() > 1) { |
| 3930 |
SmallVector<StringRef, 6> SameLenNames; |
| 3931 |
StringRef FSName = FS.name(); |
| 3932 |
llvm::copy_if( |
| 3933 |
Names, std::back_inserter(SameLenNames), |
| 3934 |
[&](StringRef N) { return N.size() == FSName.size(); }); |
| 3935 |
|
| 3936 |
if (SameLenNames.size() == 1) { |
| 3937 |
OS << "Name.size() == " << FS.name().size() << " && "; |
| 3938 |
} else { |
| 3939 |
// FIXME: We currently fall back to comparing entire strings if there |
| 3940 |
// are 2 or more spelling names with the same length. This can be |
| 3941 |
// optimized to check only for the the first differing character |
| 3942 |
// between them instead. |
| 3943 |
OS << "Name == \"" << FS.name() << "\"" |
| 3944 |
<< " && "; |
| 3945 |
} |
| 3946 |
} |
| 3947 |
|
| 3948 |
OS << "getSyntax() == AttributeCommonInfo::AS_" << FS.variety() |
| 3949 |
<< " && ComputedScope == "; |
| 3950 |
if (FS.nameSpace() == "") |
| 3951 |
OS << "AttributeCommonInfo::Scope::NONE"; |
| 3952 |
else |
| 3953 |
OS << "AttributeCommonInfo::Scope::" + FS.nameSpace().upper(); |
| 3954 |
|
| 3955 |
OS << ")\n" |
| 3956 |
<< " return " << Idx << ";\n"; |
| 3957 |
} |
| 3958 |
|
| 3959 |
OS << " break;\n" |
| 3960 |
<< " }\n"; |
| 3961 |
} |
| 3962 |
|
| 3963 |
OS << " }\n" |
| 3964 |
<< " return 0;\n"; |
| 3965 |
} |
| 3966 |
|
| 3967 |
// Emits code used by RecursiveASTVisitor to visit attributes |
| 3968 |
void EmitClangAttrASTVisitor(const RecordKeeper &Records, raw_ostream &OS) { |
| 3969 |
emitSourceFileHeader("Used by RecursiveASTVisitor to visit attributes.", OS, |
| 3970 |
Records); |
| 3971 |
// Write method declarations for Traverse* methods. |
| 3972 |
// We emit this here because we only generate methods for attributes that |
| 3973 |
// are declared as ASTNodes. |
| 3974 |
OS << "#ifdef ATTR_VISITOR_DECLS_ONLY\n\n"; |
| 3975 |
ArrayRef<const Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); |
| 3976 |
for (const auto *Attr : Attrs) { |
| 3977 |
const Record &R = *Attr; |
| 3978 |
if (!R.getValueAsBit("ASTNode")) |
| 3979 |
continue; |
| 3980 |
OS << " bool Traverse" |
| 3981 |
<< R.getName() << "Attr(" << R.getName() << "Attr *A);\n"; |
| 3982 |
OS << " bool Visit" |
| 3983 |
<< R.getName() << "Attr(" << R.getName() << "Attr *A) {\n" |
| 3984 |
<< " return true; \n" |
| 3985 |
<< " }\n"; |
| 3986 |
} |
| 3987 |
OS << "\n#else // ATTR_VISITOR_DECLS_ONLY\n\n"; |
| 3988 |
|
| 3989 |
// Write individual Traverse* methods for each attribute class. |
| 3990 |
for (const auto *Attr : Attrs) { |
| 3991 |
const Record &R = *Attr; |
| 3992 |
if (!R.getValueAsBit("ASTNode")) |
| 3993 |
continue; |
| 3994 |
|
| 3995 |
OS << "template <typename Derived>\n" |
| 3996 |
<< "bool VISITORCLASS<Derived>::Traverse" |
| 3997 |
<< R.getName() << "Attr(" << R.getName() << "Attr *A) {\n" |
| 3998 |
<< " if (!getDerived().VisitAttr(A))\n" |
| 3999 |
<< " return false;\n" |
| 4000 |
<< " if (!getDerived().Visit" << R.getName() << "Attr(A))\n" |
| 4001 |
<< " return false;\n"; |
| 4002 |
|
| 4003 |
for (const auto *Arg : R.getValueAsListOfDefs("Args")) |
| 4004 |
createArgument(*Arg, R.getName())->writeASTVisitorTraversal(OS); |
| 4005 |
|
| 4006 |
if (Attr->getValueAsBit("AcceptsExprPack")) |
| 4007 |
VariadicExprArgument("DelayedArgs", R.getName()) |
| 4008 |
.writeASTVisitorTraversal(OS); |
| 4009 |
|
| 4010 |
OS << " return true;\n"; |
| 4011 |
OS << "}\n\n"; |
| 4012 |
} |
| 4013 |
|
| 4014 |
// Write generic Traverse routine |
| 4015 |
OS << "template <typename Derived>\n" |
| 4016 |
<< "bool VISITORCLASS<Derived>::TraverseAttr(Attr *A) {\n" |
| 4017 |
<< " if (!A)\n" |
| 4018 |
<< " return true;\n" |
| 4019 |
<< "\n" |
| 4020 |
<< " switch (A->getKind()) {\n"; |
| 4021 |
|
| 4022 |
for (const auto *Attr : Attrs) { |
| 4023 |
const Record &R = *Attr; |
| 4024 |
if (!R.getValueAsBit("ASTNode")) |
| 4025 |
continue; |
| 4026 |
|
| 4027 |
OS << " case attr::" << R.getName() << ":\n" |
| 4028 |
<< " return getDerived().Traverse" << R.getName() << "Attr(" |
| 4029 |
<< "cast<" << R.getName() << "Attr>(A));\n"; |
| 4030 |
} |
| 4031 |
OS << " }\n"; // end switch |
| 4032 |
OS << " llvm_unreachable(\"bad attribute kind\");\n"; |
| 4033 |
OS << "}\n"; // end function |
| 4034 |
OS << "#endif // ATTR_VISITOR_DECLS_ONLY\n"; |
| 4035 |
} |
| 4036 |
|
| 4037 |
static void |
| 4038 |
EmitClangAttrTemplateInstantiateHelper(ArrayRef<const Record *> Attrs, |
| 4039 |
raw_ostream &OS, bool AppliesToDecl) { |
| 4040 |
|
| 4041 |
OS << " switch (At->getKind()) {\n"; |
| 4042 |
for (const auto *Attr : Attrs) { |
| 4043 |
const Record &R = *Attr; |
| 4044 |
if (!R.getValueAsBit("ASTNode")) |
| 4045 |
continue; |
| 4046 |
OS << " case attr::" << R.getName() << ": {\n"; |
| 4047 |
bool ShouldClone = R.getValueAsBit("Clone") && |
| 4048 |
(!AppliesToDecl || |
| 4049 |
R.getValueAsBit("MeaningfulToClassTemplateDefinition")); |
| 4050 |
|
| 4051 |
if (!ShouldClone) { |
| 4052 |
OS << " return nullptr;\n"; |
| 4053 |
OS << " }\n"; |
| 4054 |
continue; |
| 4055 |
} |
| 4056 |
|
| 4057 |
OS << " const auto *A = cast<" |
| 4058 |
<< R.getName() << "Attr>(At);\n"; |
| 4059 |
bool TDependent = R.getValueAsBit("TemplateDependent"); |
| 4060 |
|
| 4061 |
if (!TDependent) { |
| 4062 |
OS << " return A->clone(C);\n"; |
| 4063 |
OS << " }\n"; |
| 4064 |
continue; |
| 4065 |
} |
| 4066 |
|
| 4067 |
std::vector<const Record *> ArgRecords = R.getValueAsListOfDefs("Args"); |
| 4068 |
std::vector<std::unique_ptr<Argument>> Args; |
| 4069 |
Args.reserve(ArgRecords.size()); |
| 4070 |
|
| 4071 |
for (const auto *ArgRecord : ArgRecords) |
| 4072 |
Args.emplace_back(createArgument(*ArgRecord, R.getName())); |
| 4073 |
|
| 4074 |
for (auto const &ai : Args) |
| 4075 |
ai->writeTemplateInstantiation(OS); |
| 4076 |
|
| 4077 |
OS << " return new (C) " << R.getName() << "Attr(C, *A"; |
| 4078 |
for (auto const &ai : Args) { |
| 4079 |
OS << ", "; |
| 4080 |
ai->writeTemplateInstantiationArgs(OS); |
| 4081 |
} |
| 4082 |
OS << ");\n" |
| 4083 |
<< " }\n"; |
| 4084 |
} |
| 4085 |
OS << " } // end switch\n" |
| 4086 |
<< " llvm_unreachable(\"Unknown attribute!\");\n" |
| 4087 |
<< " return nullptr;\n"; |
| 4088 |
} |
| 4089 |
|
| 4090 |
// Emits code to instantiate dependent attributes on templates. |
| 4091 |
void EmitClangAttrTemplateInstantiate(const RecordKeeper &Records, |
| 4092 |
raw_ostream &OS) { |
| 4093 |
emitSourceFileHeader("Template instantiation code for attributes", OS, |
| 4094 |
Records); |
| 4095 |
|
| 4096 |
ArrayRef<const Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); |
| 4097 |
|
| 4098 |
OS << "namespace clang {\n" |
| 4099 |
<< "namespace sema {\n\n" |
| 4100 |
<< "Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, " |
| 4101 |
<< "Sema &S,\n" |
| 4102 |
<< " const MultiLevelTemplateArgumentList &TemplateArgs) {\n"; |
| 4103 |
EmitClangAttrTemplateInstantiateHelper(Attrs, OS, /*AppliesToDecl*/false); |
| 4104 |
OS << "}\n\n" |
| 4105 |
<< "Attr *instantiateTemplateAttributeForDecl(const Attr *At,\n" |
| 4106 |
<< " ASTContext &C, Sema &S,\n" |
| 4107 |
<< " const MultiLevelTemplateArgumentList &TemplateArgs) {\n"; |
| 4108 |
EmitClangAttrTemplateInstantiateHelper(Attrs, OS, /*AppliesToDecl*/true); |
| 4109 |
OS << "}\n\n" |
| 4110 |
<< "} // end namespace sema\n" |
| 4111 |
<< "} // end namespace clang\n"; |
| 4112 |
} |
| 4113 |
|
| 4114 |
// Emits the list of parsed attributes. |
| 4115 |
void EmitClangAttrParsedAttrList(const RecordKeeper &Records, raw_ostream &OS) { |
| 4116 |
emitSourceFileHeader("List of all attributes that Clang recognizes", OS, |
| 4117 |
Records); |
| 4118 |
|
| 4119 |
OS << "#ifndef PARSED_ATTR\n"; |
| 4120 |
OS << "#define PARSED_ATTR(NAME) NAME\n"; |
| 4121 |
OS << "#endif\n\n"; |
| 4122 |
|
| 4123 |
ParsedAttrMap Names = getParsedAttrList(Records); |
| 4124 |
for (const auto &I : Names) { |
| 4125 |
OS << "PARSED_ATTR(" << I.first << ")\n"; |
| 4126 |
} |
| 4127 |
} |
| 4128 |
|
| 4129 |
void EmitAttributeSpellingList(const RecordKeeper &Records, raw_ostream &OS) { |
| 4130 |
emitSourceFileHeader("List of attribute names", OS, Records); |
| 4131 |
|
| 4132 |
std::set<StringRef> AttrSpellingList; |
| 4133 |
std::set<StringRef> AttrScopeSpellingList; |
| 4134 |
|
| 4135 |
for (const auto *A : Records.getAllDerivedDefinitions("Attr")) { |
| 4136 |
for (const auto &S : GetFlattenedSpellings(*A)) { |
| 4137 |
AttrSpellingList.insert(S.name()); |
| 4138 |
if (S.nameSpace().size()) |
| 4139 |
AttrScopeSpellingList.insert(S.nameSpace()); |
| 4140 |
} |
| 4141 |
} |
| 4142 |
|
| 4143 |
OS << "#ifndef ATTR_NAME" << "\n"; |
| 4144 |
OS << "#define ATTR_NAME(NAME) NAME" << "\n"; |
| 4145 |
OS << "#endif" << "\n" << "\n"; |
| 4146 |
for (const auto &AttrName : AttrSpellingList) { |
| 4147 |
OS << "ATTR_NAME(\"" << AttrName << "\")\n"; |
| 4148 |
} |
| 4149 |
OS << "\n"; |
| 4150 |
OS << "#undef ATTR_NAME" << "\n"; |
| 4151 |
OS << "\n"; |
| 4152 |
|
| 4153 |
OS << "#ifndef ATTR_SCOPE_NAME" << "\n"; |
| 4154 |
OS << "#define ATTR_SCOPE_NAME(SCOPE_NAME) SCOPE_NAME" << "\n"; |
| 4155 |
OS << "#endif" << "\n" << "\n"; |
| 4156 |
for (const auto &AttrScopeName : AttrScopeSpellingList) { |
| 4157 |
OS << "ATTR_SCOPE_NAME(\"" << AttrScopeName << "\")\n"; |
| 4158 |
} |
| 4159 |
OS << "\n"; |
| 4160 |
OS << "#undef ATTR_SCOPE_NAME" << "\n"; |
| 4161 |
OS << "\n"; |
| 4162 |
} |
| 4163 |
|
| 4164 |
static bool isArgVariadic(const Record &R, StringRef AttrName) { |
| 4165 |
return createArgument(R, AttrName)->isVariadic(); |
| 4166 |
} |
| 4167 |
|
| 4168 |
static void emitArgInfo(const Record &R, raw_ostream &OS) { |
| 4169 |
// This function will count the number of arguments specified for the |
| 4170 |
// attribute and emit the number of required arguments followed by the |
| 4171 |
// number of optional arguments. |
| 4172 |
unsigned ArgCount = 0, OptCount = 0, ArgMemberCount = 0; |
| 4173 |
bool HasVariadic = false; |
| 4174 |
for (const auto *Arg : R.getValueAsListOfDefs("Args")) { |
| 4175 |
// If the arg is fake, it's the user's job to supply it: general parsing |
| 4176 |
// logic shouldn't need to know anything about it. |
| 4177 |
if (Arg->getValueAsBit("Fake")) |
| 4178 |
continue; |
| 4179 |
Arg->getValueAsBit("Optional") ? ++OptCount : ++ArgCount; |
| 4180 |
++ArgMemberCount; |
| 4181 |
if (!HasVariadic && isArgVariadic(*Arg, R.getName())) |
| 4182 |
HasVariadic = true; |
| 4183 |
} |
| 4184 |
|
| 4185 |
// If there is a variadic argument, we will set the optional argument count |
| 4186 |
// to its largest value. Since it's currently a 4-bit number, we set it to 15. |
| 4187 |
OS << " /*NumArgs=*/" << ArgCount << ",\n"; |
| 4188 |
OS << " /*OptArgs=*/" << (HasVariadic ? 15 : OptCount) << ",\n"; |
| 4189 |
OS << " /*NumArgMembers=*/" << ArgMemberCount << ",\n"; |
| 4190 |
} |
| 4191 |
|
| 4192 |
static std::string GetDiagnosticSpelling(const Record &R) { |
| 4193 |
StringRef Ret = R.getValueAsString("DiagSpelling"); |
| 4194 |
if (!Ret.empty()) |
| 4195 |
return Ret.str(); |
| 4196 |
|
| 4197 |
// If we couldn't find the DiagSpelling in this object, we can check to see |
| 4198 |
// if the object is one that has a base, and if it is, loop up to the Base |
| 4199 |
// member recursively. |
| 4200 |
if (auto Base = R.getValueAsOptionalDef(BaseFieldName)) |
| 4201 |
return GetDiagnosticSpelling(*Base); |
| 4202 |
|
| 4203 |
return ""; |
| 4204 |
} |
| 4205 |
|
| 4206 |
static std::string CalculateDiagnostic(const Record &S) { |
| 4207 |
// If the SubjectList object has a custom diagnostic associated with it, |
| 4208 |
// return that directly. |
| 4209 |
const StringRef CustomDiag = S.getValueAsString("CustomDiag"); |
| 4210 |
if (!CustomDiag.empty()) |
| 4211 |
return ("\"" + Twine(CustomDiag) + "\"").str(); |
| 4212 |
|
| 4213 |
std::vector<std::string> DiagList; |
| 4214 |
for (const auto *Subject : S.getValueAsListOfDefs("Subjects")) { |
| 4215 |
const Record &R = *Subject; |
| 4216 |
// Get the diagnostic text from the Decl or Stmt node given. |
| 4217 |
std::string V = GetDiagnosticSpelling(R); |
| 4218 |
if (V.empty()) { |
| 4219 |
PrintError(R.getLoc(), |
| 4220 |
"Could not determine diagnostic spelling for the node: " + |
| 4221 |
R.getName() + "; please add one to DeclNodes.td"); |
| 4222 |
} else { |
| 4223 |
// The node may contain a list of elements itself, so split the elements |
| 4224 |
// by a comma, and trim any whitespace. |
| 4225 |
SmallVector<StringRef, 2> Frags; |
| 4226 |
SplitString(V, Frags, ","); |
| 4227 |
for (auto Str : Frags) { |
| 4228 |
DiagList.push_back(Str.trim().str()); |
| 4229 |
} |
| 4230 |
} |
| 4231 |
} |
| 4232 |
|
| 4233 |
if (DiagList.empty()) { |
| 4234 |
PrintFatalError(S.getLoc(), |
| 4235 |
"Could not deduce diagnostic argument for Attr subjects"); |
| 4236 |
return ""; |
| 4237 |
} |
| 4238 |
|
| 4239 |
// FIXME: this is not particularly good for localization purposes and ideally |
| 4240 |
// should be part of the diagnostics engine itself with some sort of list |
| 4241 |
// specifier. |
| 4242 |
|
| 4243 |
// A single member of the list can be returned directly. |
| 4244 |
if (DiagList.size() == 1) |
| 4245 |
return '"' + DiagList.front() + '"'; |
| 4246 |
|
| 4247 |
if (DiagList.size() == 2) |
| 4248 |
return '"' + DiagList[0] + " and " + DiagList[1] + '"'; |
| 4249 |
|
| 4250 |
// If there are more than two in the list, we serialize the first N - 1 |
| 4251 |
// elements with a comma. This leaves the string in the state: foo, bar, |
| 4252 |
// baz (but misses quux). We can then add ", and " for the last element |
| 4253 |
// manually. |
| 4254 |
std::string Diag = join(DiagList.begin(), DiagList.end() - 1, ", "); |
| 4255 |
return '"' + Diag + ", and " + *(DiagList.end() - 1) + '"'; |
| 4256 |
} |
| 4257 |
|
| 4258 |
static std::string GetSubjectWithSuffix(const Record *R) { |
| 4259 |
const std::string B = R->getName().str(); |
| 4260 |
if (B == "DeclBase") |
| 4261 |
return "Decl"; |
| 4262 |
return B + "Decl"; |
| 4263 |
} |
| 4264 |
|
| 4265 |
static std::string functionNameForCustomAppertainsTo(const Record &Subject) { |
| 4266 |
return "is" + Subject.getName().str(); |
| 4267 |
} |
| 4268 |
|
| 4269 |
static void GenerateCustomAppertainsTo(const Record &Subject, raw_ostream &OS) { |
| 4270 |
std::string FnName = functionNameForCustomAppertainsTo(Subject); |
| 4271 |
|
| 4272 |
// If this code has already been generated, we don't need to do anything. |
| 4273 |
static std::set<std::string> CustomSubjectSet; |
| 4274 |
auto I = CustomSubjectSet.find(FnName); |
| 4275 |
if (I != CustomSubjectSet.end()) |
| 4276 |
return; |
| 4277 |
|
| 4278 |
// This only works with non-root Decls. |
| 4279 |
const Record *Base = Subject.getValueAsDef(BaseFieldName); |
| 4280 |
|
| 4281 |
// Not currently support custom subjects within custom subjects. |
| 4282 |
if (Base->isSubClassOf("SubsetSubject")) { |
| 4283 |
PrintFatalError(Subject.getLoc(), |
| 4284 |
"SubsetSubjects within SubsetSubjects is not supported"); |
| 4285 |
return; |
| 4286 |
} |
| 4287 |
|
| 4288 |
OS << "static bool " << FnName << "(const Decl *D) {\n"; |
| 4289 |
OS << " if (const auto *S = dyn_cast<"; |
| 4290 |
OS << GetSubjectWithSuffix(Base); |
| 4291 |
OS << ">(D))\n"; |
| 4292 |
OS << " return " << Subject.getValueAsString("CheckCode") << ";\n"; |
| 4293 |
OS << " return false;\n"; |
| 4294 |
OS << "}\n\n"; |
| 4295 |
|
| 4296 |
CustomSubjectSet.insert(FnName); |
| 4297 |
} |
| 4298 |
|
| 4299 |
static void GenerateAppertainsTo(const Record &Attr, raw_ostream &OS) { |
| 4300 |
// If the attribute does not contain a Subjects definition, then use the |
| 4301 |
// default appertainsTo logic. |
| 4302 |
if (Attr.isValueUnset("Subjects")) |
| 4303 |
return; |
| 4304 |
|
| 4305 |
const Record *SubjectObj = Attr.getValueAsDef("Subjects"); |
| 4306 |
std::vector<const Record *> Subjects = |
| 4307 |
SubjectObj->getValueAsListOfDefs("Subjects"); |
| 4308 |
|
| 4309 |
// If the list of subjects is empty, it is assumed that the attribute |
| 4310 |
// appertains to everything. |
| 4311 |
if (Subjects.empty()) |
| 4312 |
return; |
| 4313 |
|
| 4314 |
bool Warn = SubjectObj->getValueAsDef("Diag")->getValueAsBit("Warn"); |
| 4315 |
|
| 4316 |
// Split the subjects into declaration subjects and statement subjects. |
| 4317 |
// FIXME: subset subjects are added to the declaration list until there are |
| 4318 |
// enough statement attributes with custom subject needs to warrant |
| 4319 |
// the implementation effort. |
| 4320 |
std::vector<const Record *> DeclSubjects, StmtSubjects; |
| 4321 |
copy_if(Subjects, std::back_inserter(DeclSubjects), [](const Record *R) { |
| 4322 |
return R->isSubClassOf("SubsetSubject") || !R->isSubClassOf("StmtNode"); |
| 4323 |
}); |
| 4324 |
copy_if(Subjects, std::back_inserter(StmtSubjects), |
| 4325 |
[](const Record *R) { return R->isSubClassOf("StmtNode"); }); |
| 4326 |
|
| 4327 |
// We should have sorted all of the subjects into two lists. |
| 4328 |
// FIXME: this assertion will be wrong if we ever add type attribute subjects. |
| 4329 |
assert(DeclSubjects.size() + StmtSubjects.size() == Subjects.size()); |
| 4330 |
|
| 4331 |
if (DeclSubjects.empty()) { |
| 4332 |
// If there are no decl subjects but there are stmt subjects, diagnose |
| 4333 |
// trying to apply a statement attribute to a declaration. |
| 4334 |
if (!StmtSubjects.empty()) { |
| 4335 |
OS << "bool diagAppertainsToDecl(Sema &S, const ParsedAttr &AL, "; |
| 4336 |
OS << "const Decl *D) const override {\n"; |
| 4337 |
OS << " S.Diag(AL.getLoc(), diag::err_attribute_invalid_on_decl)\n"; |
| 4338 |
OS << " << AL << AL.isRegularKeywordAttribute() << " |
| 4339 |
"D->getLocation();\n"; |
| 4340 |
OS << " return false;\n"; |
| 4341 |
OS << "}\n\n"; |
| 4342 |
} |
| 4343 |
} else { |
| 4344 |
// Otherwise, generate an appertainsTo check specific to this attribute |
| 4345 |
// which checks all of the given subjects against the Decl passed in. |
| 4346 |
OS << "bool diagAppertainsToDecl(Sema &S, "; |
| 4347 |
OS << "const ParsedAttr &Attr, const Decl *D) const override {\n"; |
| 4348 |
OS << " if ("; |
| 4349 |
for (auto I = DeclSubjects.begin(), E = DeclSubjects.end(); I != E; ++I) { |
| 4350 |
// If the subject has custom code associated with it, use the generated |
| 4351 |
// function for it. The function cannot be inlined into this check (yet) |
| 4352 |
// because it requires the subject to be of a specific type, and were that |
| 4353 |
// information inlined here, it would not support an attribute with |
| 4354 |
// multiple custom subjects. |
| 4355 |
if ((*I)->isSubClassOf("SubsetSubject")) |
| 4356 |
OS << "!" << functionNameForCustomAppertainsTo(**I) << "(D)"; |
| 4357 |
else |
| 4358 |
OS << "!isa<" << GetSubjectWithSuffix(*I) << ">(D)"; |
| 4359 |
|
| 4360 |
if (I + 1 != E) |
| 4361 |
OS << " && "; |
| 4362 |
} |
| 4363 |
OS << ") {\n"; |
| 4364 |
OS << " S.Diag(Attr.getLoc(), diag::"; |
| 4365 |
OS << (Warn ? "warn_attribute_wrong_decl_type_str" |
| 4366 |
: "err_attribute_wrong_decl_type_str"); |
| 4367 |
OS << ")\n"; |
| 4368 |
OS << " << Attr << Attr.isRegularKeywordAttribute() << "; |
| 4369 |
OS << CalculateDiagnostic(*SubjectObj) << ";\n"; |
| 4370 |
OS << " return false;\n"; |
| 4371 |
OS << " }\n"; |
| 4372 |
OS << " return true;\n"; |
| 4373 |
OS << "}\n\n"; |
| 4374 |
} |
| 4375 |
|
| 4376 |
if (StmtSubjects.empty()) { |
| 4377 |
// If there are no stmt subjects but there are decl subjects, diagnose |
| 4378 |
// trying to apply a declaration attribute to a statement. |
| 4379 |
if (!DeclSubjects.empty()) { |
| 4380 |
OS << "bool diagAppertainsToStmt(Sema &S, const ParsedAttr &AL, "; |
| 4381 |
OS << "const Stmt *St) const override {\n"; |
| 4382 |
OS << " S.Diag(AL.getLoc(), diag::err_decl_attribute_invalid_on_stmt)\n"; |
| 4383 |
OS << " << AL << AL.isRegularKeywordAttribute() << " |
| 4384 |
"St->getBeginLoc();\n"; |
| 4385 |
OS << " return false;\n"; |
| 4386 |
OS << "}\n\n"; |
| 4387 |
} |
| 4388 |
} else { |
| 4389 |
// Now, do the same for statements. |
| 4390 |
OS << "bool diagAppertainsToStmt(Sema &S, "; |
| 4391 |
OS << "const ParsedAttr &Attr, const Stmt *St) const override {\n"; |
| 4392 |
OS << " if ("; |
| 4393 |
for (auto I = StmtSubjects.begin(), E = StmtSubjects.end(); I != E; ++I) { |
| 4394 |
OS << "!isa<" << (*I)->getName() << ">(St)"; |
| 4395 |
if (I + 1 != E) |
| 4396 |
OS << " && "; |
| 4397 |
} |
| 4398 |
OS << ") {\n"; |
| 4399 |
OS << " S.Diag(Attr.getLoc(), diag::"; |
| 4400 |
OS << (Warn ? "warn_attribute_wrong_decl_type_str" |
| 4401 |
: "err_attribute_wrong_decl_type_str"); |
| 4402 |
OS << ")\n"; |
| 4403 |
OS << " << Attr << Attr.isRegularKeywordAttribute() << "; |
| 4404 |
OS << CalculateDiagnostic(*SubjectObj) << ";\n"; |
| 4405 |
OS << " return false;\n"; |
| 4406 |
OS << " }\n"; |
| 4407 |
OS << " return true;\n"; |
| 4408 |
OS << "}\n\n"; |
| 4409 |
} |
| 4410 |
} |
| 4411 |
|
| 4412 |
// Generates the mutual exclusion checks. The checks for parsed attributes are |
| 4413 |
// written into OS and the checks for merging declaration attributes are |
| 4414 |
// written into MergeOS. |
| 4415 |
static void GenerateMutualExclusionsChecks(const Record &Attr, |
| 4416 |
const RecordKeeper &Records, |
| 4417 |
raw_ostream &OS, |
| 4418 |
raw_ostream &MergeDeclOS, |
| 4419 |
raw_ostream &MergeStmtOS) { |
| 4420 |
// We don't do any of this magic for type attributes yet. |
| 4421 |
if (Attr.isSubClassOf("TypeAttr")) |
| 4422 |
return; |
| 4423 |
|
| 4424 |
// This means the attribute is either a statement attribute, a decl |
| 4425 |
// attribute, or both; find out which. |
| 4426 |
bool CurAttrIsStmtAttr = Attr.isSubClassOf("StmtAttr") || |
| 4427 |
Attr.isSubClassOf("DeclOrStmtAttr") || |
| 4428 |
Attr.isSubClassOf("InheritableParamOrStmtAttr"); |
| 4429 |
bool CurAttrIsDeclAttr = !CurAttrIsStmtAttr || |
| 4430 |
Attr.isSubClassOf("DeclOrStmtAttr") || |
| 4431 |
Attr.isSubClassOf("InheritableParamOrStmtAttr"); |
| 4432 |
|
| 4433 |
std::vector<std::string> DeclAttrs, StmtAttrs; |
| 4434 |
|
| 4435 |
// Find all of the definitions that inherit from MutualExclusions and include |
| 4436 |
// the given attribute in the list of exclusions to generate the |
| 4437 |
// diagMutualExclusion() check. |
| 4438 |
for (const Record *Exclusion : |
| 4439 |
Records.getAllDerivedDefinitions("MutualExclusions")) { |
| 4440 |
std::vector<const Record *> MutuallyExclusiveAttrs = |
| 4441 |
Exclusion->getValueAsListOfDefs("Exclusions"); |
| 4442 |
auto IsCurAttr = [Attr](const Record *R) { |
| 4443 |
return R->getName() == Attr.getName(); |
| 4444 |
}; |
| 4445 |
if (any_of(MutuallyExclusiveAttrs, IsCurAttr)) { |
| 4446 |
// This list of exclusions includes the attribute we're looking for, so |
| 4447 |
// add the exclusive attributes to the proper list for checking. |
| 4448 |
for (const Record *AttrToExclude : MutuallyExclusiveAttrs) { |
| 4449 |
if (IsCurAttr(AttrToExclude)) |
| 4450 |
continue; |
| 4451 |
|
| 4452 |
if (CurAttrIsStmtAttr) |
| 4453 |
StmtAttrs.push_back((AttrToExclude->getName() + "Attr").str()); |
| 4454 |
if (CurAttrIsDeclAttr) |
| 4455 |
DeclAttrs.push_back((AttrToExclude->getName() + "Attr").str()); |
| 4456 |
} |
| 4457 |
} |
| 4458 |
} |
| 4459 |
|
| 4460 |
// If there are any decl or stmt attributes, silence -Woverloaded-virtual |
| 4461 |
// warnings for them both. |
| 4462 |
if (!DeclAttrs.empty() || !StmtAttrs.empty()) |
| 4463 |
OS << " using ParsedAttrInfo::diagMutualExclusion;\n\n"; |
| 4464 |
|
| 4465 |
// If we discovered any decl or stmt attributes to test for, generate the |
| 4466 |
// predicates for them now. |
| 4467 |
if (!DeclAttrs.empty()) { |
| 4468 |
// Generate the ParsedAttrInfo subclass logic for declarations. |
| 4469 |
OS << " bool diagMutualExclusion(Sema &S, const ParsedAttr &AL, " |
| 4470 |
<< "const Decl *D) const override {\n"; |
| 4471 |
for (const std::string &A : DeclAttrs) { |
| 4472 |
OS << " if (const auto *A = D->getAttr<" << A << ">()) {\n"; |
| 4473 |
OS << " S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)" |
| 4474 |
<< " << AL << A << (AL.isRegularKeywordAttribute() ||" |
| 4475 |
<< " A->isRegularKeywordAttribute());\n"; |
| 4476 |
OS << " S.Diag(A->getLocation(), diag::note_conflicting_attribute);"; |
| 4477 |
OS << " \nreturn false;\n"; |
| 4478 |
OS << " }\n"; |
| 4479 |
} |
| 4480 |
OS << " return true;\n"; |
| 4481 |
OS << " }\n\n"; |
| 4482 |
|
| 4483 |
// Also generate the declaration attribute merging logic if the current |
| 4484 |
// attribute is one that can be inheritted on a declaration. It is assumed |
| 4485 |
// this code will be executed in the context of a function with parameters: |
| 4486 |
// Sema &S, Decl *D, Attr *A and that returns a bool (false on diagnostic, |
| 4487 |
// true on success). |
| 4488 |
if (Attr.isSubClassOf("InheritableAttr")) { |
| 4489 |
MergeDeclOS << " if (const auto *Second = dyn_cast<" |
| 4490 |
<< (Attr.getName() + "Attr").str() << ">(A)) {\n"; |
| 4491 |
for (const std::string &A : DeclAttrs) { |
| 4492 |
MergeDeclOS << " if (const auto *First = D->getAttr<" << A |
| 4493 |
<< ">()) {\n"; |
| 4494 |
MergeDeclOS << " S.Diag(First->getLocation(), " |
| 4495 |
<< "diag::err_attributes_are_not_compatible) << First << " |
| 4496 |
<< "Second << (First->isRegularKeywordAttribute() || " |
| 4497 |
<< "Second->isRegularKeywordAttribute());\n"; |
| 4498 |
MergeDeclOS << " S.Diag(Second->getLocation(), " |
| 4499 |
<< "diag::note_conflicting_attribute);\n"; |
| 4500 |
MergeDeclOS << " return false;\n"; |
| 4501 |
MergeDeclOS << " }\n"; |
| 4502 |
} |
| 4503 |
MergeDeclOS << " return true;\n"; |
| 4504 |
MergeDeclOS << " }\n"; |
| 4505 |
} |
| 4506 |
} |
| 4507 |
|
| 4508 |
// Statement attributes are a bit different from declarations. With |
| 4509 |
// declarations, each attribute is added to the declaration as it is |
| 4510 |
// processed, and so you can look on the Decl * itself to see if there is a |
| 4511 |
// conflicting attribute. Statement attributes are processed as a group |
| 4512 |
// because AttributedStmt needs to tail-allocate all of the attribute nodes |
| 4513 |
// at once. This means we cannot check whether the statement already contains |
| 4514 |
// an attribute to check for the conflict. Instead, we need to check whether |
| 4515 |
// the given list of semantic attributes contain any conflicts. It is assumed |
| 4516 |
// this code will be executed in the context of a function with parameters: |
| 4517 |
// Sema &S, const SmallVectorImpl<const Attr *> &C. The code will be within a |
| 4518 |
// loop which loops over the container C with a loop variable named A to |
| 4519 |
// represent the current attribute to check for conflicts. |
| 4520 |
// |
| 4521 |
// FIXME: it would be nice not to walk over the list of potential attributes |
| 4522 |
// to apply to the statement more than once, but statements typically don't |
| 4523 |
// have long lists of attributes on them, so re-walking the list should not |
| 4524 |
// be an expensive operation. |
| 4525 |
if (!StmtAttrs.empty()) { |
| 4526 |
MergeStmtOS << " if (const auto *Second = dyn_cast<" |
| 4527 |
<< (Attr.getName() + "Attr").str() << ">(A)) {\n"; |
| 4528 |
MergeStmtOS << " auto Iter = llvm::find_if(C, [](const Attr *Check) " |
| 4529 |
<< "{ return isa<"; |
| 4530 |
interleave( |
| 4531 |
StmtAttrs, [&](StringRef Name) { MergeStmtOS << Name; }, |
| 4532 |
[&] { MergeStmtOS << ", "; }); |
| 4533 |
MergeStmtOS << ">(Check); });\n"; |
| 4534 |
MergeStmtOS << " if (Iter != C.end()) {\n"; |
| 4535 |
MergeStmtOS << " S.Diag((*Iter)->getLocation(), " |
| 4536 |
<< "diag::err_attributes_are_not_compatible) << *Iter << " |
| 4537 |
<< "Second << ((*Iter)->isRegularKeywordAttribute() || " |
| 4538 |
<< "Second->isRegularKeywordAttribute());\n"; |
| 4539 |
MergeStmtOS << " S.Diag(Second->getLocation(), " |
| 4540 |
<< "diag::note_conflicting_attribute);\n"; |
| 4541 |
MergeStmtOS << " return false;\n"; |
| 4542 |
MergeStmtOS << " }\n"; |
| 4543 |
MergeStmtOS << " }\n"; |
| 4544 |
} |
| 4545 |
} |
| 4546 |
|
| 4547 |
static void |
| 4548 |
emitAttributeMatchRules(PragmaClangAttributeSupport &PragmaAttributeSupport, |
| 4549 |
raw_ostream &OS) { |
| 4550 |
OS << "static bool checkAttributeMatchRuleAppliesTo(const Decl *D, " |
| 4551 |
<< AttributeSubjectMatchRule::EnumName << " rule) {\n"; |
| 4552 |
OS << " switch (rule) {\n"; |
| 4553 |
for (const auto &Rule : PragmaAttributeSupport.Rules) { |
| 4554 |
if (Rule.isAbstractRule()) { |
| 4555 |
OS << " case " << Rule.getEnumValue() << ":\n"; |
| 4556 |
OS << " assert(false && \"Abstract matcher rule isn't allowed\");\n"; |
| 4557 |
OS << " return false;\n"; |
| 4558 |
continue; |
| 4559 |
} |
| 4560 |
std::vector<const Record *> Subjects = Rule.getSubjects(); |
| 4561 |
assert(!Subjects.empty() && "Missing subjects"); |
| 4562 |
OS << " case " << Rule.getEnumValue() << ":\n"; |
| 4563 |
OS << " return "; |
| 4564 |
for (auto I = Subjects.begin(), E = Subjects.end(); I != E; ++I) { |
| 4565 |
// If the subject has custom code associated with it, use the function |
| 4566 |
// that was generated for GenerateAppertainsTo to check if the declaration |
| 4567 |
// is valid. |
| 4568 |
if ((*I)->isSubClassOf("SubsetSubject")) |
| 4569 |
OS << functionNameForCustomAppertainsTo(**I) << "(D)"; |
| 4570 |
else |
| 4571 |
OS << "isa<" << GetSubjectWithSuffix(*I) << ">(D)"; |
| 4572 |
|
| 4573 |
if (I + 1 != E) |
| 4574 |
OS << " || "; |
| 4575 |
} |
| 4576 |
OS << ";\n"; |
| 4577 |
} |
| 4578 |
OS << " }\n"; |
| 4579 |
OS << " llvm_unreachable(\"Invalid match rule\");\nreturn false;\n"; |
| 4580 |
OS << "}\n\n"; |
| 4581 |
} |
| 4582 |
|
| 4583 |
static void GenerateLangOptRequirements(const Record &R, |
| 4584 |
raw_ostream &OS) { |
| 4585 |
// If the attribute has an empty or unset list of language requirements, |
| 4586 |
// use the default handler. |
| 4587 |
std::vector<const Record *> LangOpts = R.getValueAsListOfDefs("LangOpts"); |
| 4588 |
if (LangOpts.empty()) |
| 4589 |
return; |
| 4590 |
|
| 4591 |
OS << "bool acceptsLangOpts(const LangOptions &LangOpts) const override {\n"; |
| 4592 |
OS << " return " << GenerateTestExpression(LangOpts) << ";\n"; |
| 4593 |
OS << "}\n\n"; |
| 4594 |
} |
| 4595 |
|
| 4596 |
static void GenerateTargetRequirements(const Record &Attr, |
| 4597 |
const ParsedAttrMap &Dupes, |
| 4598 |
raw_ostream &OS) { |
| 4599 |
// If the attribute is not a target specific attribute, use the default |
| 4600 |
// target handler. |
| 4601 |
if (!Attr.isSubClassOf("TargetSpecificAttr")) |
| 4602 |
return; |
| 4603 |
|
| 4604 |
// Get the list of architectures to be tested for. |
| 4605 |
const Record *R = Attr.getValueAsDef("Target"); |
| 4606 |
std::vector<StringRef> Arches = R->getValueAsListOfStrings("Arches"); |
| 4607 |
|
| 4608 |
// If there are other attributes which share the same parsed attribute kind, |
| 4609 |
// such as target-specific attributes with a shared spelling, collapse the |
| 4610 |
// duplicate architectures. This is required because a shared target-specific |
| 4611 |
// attribute has only one ParsedAttr::Kind enumeration value, but it |
| 4612 |
// applies to multiple target architectures. In order for the attribute to be |
| 4613 |
// considered valid, all of its architectures need to be included. |
| 4614 |
if (!Attr.isValueUnset("ParseKind")) { |
| 4615 |
const StringRef APK = Attr.getValueAsString("ParseKind"); |
| 4616 |
for (const auto &I : Dupes) { |
| 4617 |
if (I.first == APK) { |
| 4618 |
std::vector<StringRef> DA = |
| 4619 |
I.second->getValueAsDef("Target")->getValueAsListOfStrings( |
| 4620 |
"Arches"); |
| 4621 |
llvm::append_range(Arches, DA); |
| 4622 |
} |
| 4623 |
} |
| 4624 |
} |
| 4625 |
|
| 4626 |
std::string FnName = "isTarget"; |
| 4627 |
std::string Test; |
| 4628 |
bool UsesT = GenerateTargetSpecificAttrChecks(R, Arches, Test, &FnName); |
| 4629 |
|
| 4630 |
OS << "bool existsInTarget(const TargetInfo &Target) const override {\n"; |
| 4631 |
if (UsesT) |
| 4632 |
OS << " const llvm::Triple &T = Target.getTriple(); (void)T;\n"; |
| 4633 |
OS << " return " << Test << ";\n"; |
| 4634 |
OS << "}\n\n"; |
| 4635 |
} |
| 4636 |
|
| 4637 |
static void |
| 4638 |
GenerateSpellingTargetRequirements(const Record &Attr, |
| 4639 |
ArrayRef<const Record *> TargetSpellings, |
| 4640 |
raw_ostream &OS) { |
| 4641 |
// If there are no target specific spellings, use the default target handler. |
| 4642 |
if (TargetSpellings.empty()) |
| 4643 |
return; |
| 4644 |
|
| 4645 |
std::string Test; |
| 4646 |
bool UsesT = false; |
| 4647 |
const std::vector<FlattenedSpelling> SpellingList = |
| 4648 |
GetFlattenedSpellings(Attr); |
| 4649 |
for (unsigned TargetIndex = 0; TargetIndex < TargetSpellings.size(); |
| 4650 |
++TargetIndex) { |
| 4651 |
const auto &TargetSpelling = TargetSpellings[TargetIndex]; |
| 4652 |
std::vector<FlattenedSpelling> Spellings = |
| 4653 |
GetFlattenedSpellings(*TargetSpelling); |
| 4654 |
|
| 4655 |
Test += "((SpellingListIndex == "; |
| 4656 |
for (unsigned Index = 0; Index < Spellings.size(); ++Index) { |
| 4657 |
Test += itostr(getSpellingListIndex(SpellingList, Spellings[Index])); |
| 4658 |
if (Index != Spellings.size() - 1) |
| 4659 |
Test += " ||\n SpellingListIndex == "; |
| 4660 |
else |
| 4661 |
Test += ") && "; |
| 4662 |
} |
| 4663 |
|
| 4664 |
const Record *Target = TargetSpelling->getValueAsDef("Target"); |
| 4665 |
std::vector<StringRef> Arches = Target->getValueAsListOfStrings("Arches"); |
| 4666 |
std::string FnName = "isTargetSpelling"; |
| 4667 |
UsesT |= GenerateTargetSpecificAttrChecks(Target, Arches, Test, &FnName); |
| 4668 |
Test += ")"; |
| 4669 |
if (TargetIndex != TargetSpellings.size() - 1) |
| 4670 |
Test += " || "; |
| 4671 |
} |
| 4672 |
|
| 4673 |
OS << "bool spellingExistsInTarget(const TargetInfo &Target,\n"; |
| 4674 |
OS << " const unsigned SpellingListIndex) const " |
| 4675 |
"override {\n"; |
| 4676 |
if (UsesT) |
| 4677 |
OS << " const llvm::Triple &T = Target.getTriple(); (void)T;\n"; |
| 4678 |
OS << " return " << Test << ";\n", OS << "}\n\n"; |
| 4679 |
} |
| 4680 |
|
| 4681 |
static void GenerateSpellingIndexToSemanticSpelling(const Record &Attr, |
| 4682 |
raw_ostream &OS) { |
| 4683 |
// If the attribute does not have a semantic form, we can bail out early. |
| 4684 |
if (!Attr.getValueAsBit("ASTNode")) |
| 4685 |
return; |
| 4686 |
|
| 4687 |
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr); |
| 4688 |
|
| 4689 |
// If there are zero or one spellings, or all of the spellings share the same |
| 4690 |
// name, we can also bail out early. |
| 4691 |
if (Spellings.size() <= 1 || SpellingNamesAreCommon(Spellings)) |
| 4692 |
return; |
| 4693 |
|
| 4694 |
// Generate the enumeration we will use for the mapping. |
| 4695 |
SemanticSpellingMap SemanticToSyntacticMap; |
| 4696 |
std::string Enum = CreateSemanticSpellings(Spellings, SemanticToSyntacticMap); |
| 4697 |
|
| 4698 |
OS << "unsigned spellingIndexToSemanticSpelling("; |
| 4699 |
OS << "const ParsedAttr &Attr) const override {\n"; |
| 4700 |
OS << Enum; |
| 4701 |
OS << " unsigned Idx = Attr.getAttributeSpellingListIndex();\n"; |
| 4702 |
WriteSemanticSpellingSwitch("Idx", SemanticToSyntacticMap, OS); |
| 4703 |
OS << "}\n\n"; |
| 4704 |
} |
| 4705 |
|
| 4706 |
static void GenerateHandleDeclAttribute(const Record &Attr, raw_ostream &OS) { |
| 4707 |
// Only generate if Attr can be handled simply. |
| 4708 |
if (!Attr.getValueAsBit("SimpleHandler")) |
| 4709 |
return; |
| 4710 |
|
| 4711 |
// Generate a function which just converts from ParsedAttr to the Attr type. |
| 4712 |
OS << "AttrHandling handleDeclAttribute(Sema &S, Decl *D,"; |
| 4713 |
OS << "const ParsedAttr &Attr) const override {\n"; |
| 4714 |
OS << " D->addAttr(::new (S.Context) " << Attr.getName(); |
| 4715 |
OS << "Attr(S.Context, Attr));\n"; |
| 4716 |
OS << " return AttributeApplied;\n"; |
| 4717 |
OS << "}\n\n"; |
| 4718 |
} |
| 4719 |
|
| 4720 |
static bool isParamExpr(const Record *Arg) { |
| 4721 |
return !Arg->getDirectSuperClasses().empty() && |
| 4722 |
StringSwitch<bool>( |
| 4723 |
Arg->getDirectSuperClasses().back().first->getName()) |
| 4724 |
.Case("ExprArgument", true) |
| 4725 |
.Case("VariadicExprArgument", true) |
| 4726 |
.Default(false); |
| 4727 |
} |
| 4728 |
|
| 4729 |
static void GenerateIsParamExpr(const Record &Attr, raw_ostream &OS) { |
| 4730 |
OS << "bool isParamExpr(size_t N) const override {\n"; |
| 4731 |
OS << " return "; |
| 4732 |
auto Args = Attr.getValueAsListOfDefs("Args"); |
| 4733 |
for (size_t I = 0; I < Args.size(); ++I) |
| 4734 |
if (isParamExpr(Args[I])) |
| 4735 |
OS << "(N == " << I << ") || "; |
| 4736 |
OS << "false;\n"; |
| 4737 |
OS << "}\n\n"; |
| 4738 |
} |
| 4739 |
|
| 4740 |
static void GenerateHandleAttrWithDelayedArgs(const RecordKeeper &Records, |
| 4741 |
raw_ostream &OS) { |
| 4742 |
OS << "static void handleAttrWithDelayedArgs(Sema &S, Decl *D, "; |
| 4743 |
OS << "const ParsedAttr &Attr) {\n"; |
| 4744 |
OS << " SmallVector<Expr *, 4> ArgExprs;\n"; |
| 4745 |
OS << " ArgExprs.reserve(Attr.getNumArgs());\n"; |
| 4746 |
OS << " for (unsigned I = 0; I < Attr.getNumArgs(); ++I) {\n"; |
| 4747 |
OS << " assert(!Attr.isArgIdent(I));\n"; |
| 4748 |
OS << " ArgExprs.push_back(Attr.getArgAsExpr(I));\n"; |
| 4749 |
OS << " }\n"; |
| 4750 |
OS << " clang::Attr *CreatedAttr = nullptr;\n"; |
| 4751 |
OS << " switch (Attr.getKind()) {\n"; |
| 4752 |
OS << " default:\n"; |
| 4753 |
OS << " llvm_unreachable(\"Attribute cannot hold delayed arguments.\");\n"; |
| 4754 |
ParsedAttrMap Attrs = getParsedAttrList(Records); |
| 4755 |
for (const auto &I : Attrs) { |
| 4756 |
const Record &R = *I.second; |
| 4757 |
if (!R.getValueAsBit("AcceptsExprPack")) |
| 4758 |
continue; |
| 4759 |
OS << " case ParsedAttr::AT_" << I.first << ": {\n"; |
| 4760 |
OS << " CreatedAttr = " << R.getName() << "Attr::CreateWithDelayedArgs"; |
| 4761 |
OS << "(S.Context, ArgExprs.data(), ArgExprs.size(), Attr);\n"; |
| 4762 |
OS << " break;\n"; |
| 4763 |
OS << " }\n"; |
| 4764 |
} |
| 4765 |
OS << " }\n"; |
| 4766 |
OS << " D->addAttr(CreatedAttr);\n"; |
| 4767 |
OS << "}\n\n"; |
| 4768 |
} |
| 4769 |
|
| 4770 |
static bool IsKnownToGCC(const Record &Attr) { |
| 4771 |
// Look at the spellings for this subject; if there are any spellings which |
| 4772 |
// claim to be known to GCC, the attribute is known to GCC. |
| 4773 |
return any_of(GetFlattenedSpellings(Attr), |
| 4774 |
[](const FlattenedSpelling &S) { return S.knownToGCC(); }); |
| 4775 |
} |
| 4776 |
|
| 4777 |
/// Emits the parsed attribute helpers |
| 4778 |
void EmitClangAttrParsedAttrImpl(const RecordKeeper &Records, raw_ostream &OS) { |
| 4779 |
emitSourceFileHeader("Parsed attribute helpers", OS, Records); |
| 4780 |
|
| 4781 |
OS << "#if !defined(WANT_DECL_MERGE_LOGIC) && " |
| 4782 |
<< "!defined(WANT_STMT_MERGE_LOGIC)\n"; |
| 4783 |
PragmaClangAttributeSupport &PragmaAttributeSupport = |
| 4784 |
getPragmaAttributeSupport(Records); |
| 4785 |
|
| 4786 |
// Get the list of parsed attributes, and accept the optional list of |
| 4787 |
// duplicates due to the ParseKind. |
| 4788 |
ParsedAttrMap Dupes; |
| 4789 |
ParsedAttrMap Attrs = getParsedAttrList(Records, &Dupes); |
| 4790 |
|
| 4791 |
// Generate all of the custom appertainsTo functions that the attributes |
| 4792 |
// will be using. |
| 4793 |
for (const auto &I : Attrs) { |
| 4794 |
const Record &Attr = *I.second; |
| 4795 |
if (Attr.isValueUnset("Subjects")) |
| 4796 |
continue; |
| 4797 |
const Record *SubjectObj = Attr.getValueAsDef("Subjects"); |
| 4798 |
for (const Record *Subject : SubjectObj->getValueAsListOfDefs("Subjects")) |
| 4799 |
if (Subject->isSubClassOf("SubsetSubject")) |
| 4800 |
GenerateCustomAppertainsTo(*Subject, OS); |
| 4801 |
} |
| 4802 |
|
| 4803 |
// This stream is used to collect all of the declaration attribute merging |
| 4804 |
// logic for performing mutual exclusion checks. This gets emitted at the |
| 4805 |
// end of the file in a helper function of its own. |
| 4806 |
std::string DeclMergeChecks, StmtMergeChecks; |
| 4807 |
raw_string_ostream MergeDeclOS(DeclMergeChecks), MergeStmtOS(StmtMergeChecks); |
| 4808 |
|
| 4809 |
// Generate a ParsedAttrInfo struct for each of the attributes. |
| 4810 |
for (auto I = Attrs.begin(), E = Attrs.end(); I != E; ++I) { |
| 4811 |
// TODO: If the attribute's kind appears in the list of duplicates, that is |
| 4812 |
// because it is a target-specific attribute that appears multiple times. |
| 4813 |
// It would be beneficial to test whether the duplicates are "similar |
| 4814 |
// enough" to each other to not cause problems. For instance, check that |
| 4815 |
// the spellings are identical, and custom parsing rules match, etc. |
| 4816 |
|
| 4817 |
// We need to generate struct instances based off ParsedAttrInfo from |
| 4818 |
// ParsedAttr.cpp. |
| 4819 |
const std::string &AttrName = I->first; |
| 4820 |
const Record &Attr = *I->second; |
| 4821 |
auto Spellings = GetFlattenedSpellings(Attr); |
| 4822 |
if (!Spellings.empty()) { |
| 4823 |
OS << "static constexpr ParsedAttrInfo::Spelling " << I->first |
| 4824 |
<< "Spellings[] = {\n"; |
| 4825 |
for (const auto &S : Spellings) { |
| 4826 |
StringRef RawSpelling = S.name(); |
| 4827 |
std::string Spelling; |
| 4828 |
if (!S.nameSpace().empty()) |
| 4829 |
Spelling += S.nameSpace().str() + "::"; |
| 4830 |
if (S.variety() == "GNU") |
| 4831 |
Spelling += NormalizeGNUAttrSpelling(RawSpelling); |
| 4832 |
else |
| 4833 |
Spelling += RawSpelling; |
| 4834 |
OS << " {AttributeCommonInfo::AS_" << S.variety(); |
| 4835 |
OS << ", \"" << Spelling << "\"},\n"; |
| 4836 |
} |
| 4837 |
OS << "};\n"; |
| 4838 |
} |
| 4839 |
|
| 4840 |
std::vector<std::string> ArgNames; |
| 4841 |
for (const auto *Arg : Attr.getValueAsListOfDefs("Args")) { |
| 4842 |
bool UnusedUnset; |
| 4843 |
if (Arg->getValueAsBitOrUnset("Fake", UnusedUnset)) |
| 4844 |
continue; |
| 4845 |
ArgNames.push_back(Arg->getValueAsString("Name").str()); |
| 4846 |
for (const Record *Class : Arg->getSuperClasses()) { |
| 4847 |
if (Class->getName().starts_with("Variadic")) { |
| 4848 |
ArgNames.back().append("..."); |
| 4849 |
break; |
| 4850 |
} |
| 4851 |
} |
| 4852 |
} |
| 4853 |
if (!ArgNames.empty()) { |
| 4854 |
OS << "static constexpr const char *" << I->first << "ArgNames[] = {\n"; |
| 4855 |
for (const auto &N : ArgNames) |
| 4856 |
OS << '"' << N << "\","; |
| 4857 |
OS << "};\n"; |
| 4858 |
} |
| 4859 |
|
| 4860 |
OS << "struct ParsedAttrInfo" << I->first |
| 4861 |
<< " final : public ParsedAttrInfo {\n"; |
| 4862 |
OS << " constexpr ParsedAttrInfo" << I->first << "() : ParsedAttrInfo(\n"; |
| 4863 |
OS << " /*AttrKind=*/ParsedAttr::AT_" << AttrName << ",\n"; |
| 4864 |
emitArgInfo(Attr, OS); |
| 4865 |
OS << " /*HasCustomParsing=*/"; |
| 4866 |
OS << Attr.getValueAsBit("HasCustomParsing") << ",\n"; |
| 4867 |
OS << " /*AcceptsExprPack=*/"; |
| 4868 |
OS << Attr.getValueAsBit("AcceptsExprPack") << ",\n"; |
| 4869 |
OS << " /*IsTargetSpecific=*/"; |
| 4870 |
OS << Attr.isSubClassOf("TargetSpecificAttr") << ",\n"; |
| 4871 |
OS << " /*IsType=*/"; |
| 4872 |
OS << (Attr.isSubClassOf("TypeAttr") || Attr.isSubClassOf("DeclOrTypeAttr")) |
| 4873 |
<< ",\n"; |
| 4874 |
OS << " /*IsStmt=*/"; |
| 4875 |
OS << (Attr.isSubClassOf("StmtAttr") || Attr.isSubClassOf("DeclOrStmtAttr")) |
| 4876 |
<< ",\n"; |
| 4877 |
OS << " /*IsKnownToGCC=*/"; |
| 4878 |
OS << IsKnownToGCC(Attr) << ",\n"; |
| 4879 |
OS << " /*IsSupportedByPragmaAttribute=*/"; |
| 4880 |
OS << PragmaAttributeSupport.isAttributedSupported(*I->second) << ",\n"; |
| 4881 |
if (!Spellings.empty()) |
| 4882 |
OS << " /*Spellings=*/" << I->first << "Spellings,\n"; |
| 4883 |
else |
| 4884 |
OS << " /*Spellings=*/{},\n"; |
| 4885 |
if (!ArgNames.empty()) |
| 4886 |
OS << " /*ArgNames=*/" << I->first << "ArgNames"; |
| 4887 |
else |
| 4888 |
OS << " /*ArgNames=*/{}"; |
| 4889 |
OS << ") {}\n"; |
| 4890 |
GenerateAppertainsTo(Attr, OS); |
| 4891 |
GenerateMutualExclusionsChecks(Attr, Records, OS, MergeDeclOS, MergeStmtOS); |
| 4892 |
GenerateLangOptRequirements(Attr, OS); |
| 4893 |
GenerateTargetRequirements(Attr, Dupes, OS); |
| 4894 |
GenerateSpellingTargetRequirements( |
| 4895 |
Attr, Attr.getValueAsListOfDefs("TargetSpecificSpellings"), OS); |
| 4896 |
GenerateSpellingIndexToSemanticSpelling(Attr, OS); |
| 4897 |
PragmaAttributeSupport.generateStrictConformsTo(*I->second, OS); |
| 4898 |
GenerateHandleDeclAttribute(Attr, OS); |
| 4899 |
GenerateIsParamExpr(Attr, OS); |
| 4900 |
OS << "static const ParsedAttrInfo" << I->first << " Instance;\n"; |
| 4901 |
OS << "};\n"; |
| 4902 |
OS << "const ParsedAttrInfo" << I->first << " ParsedAttrInfo" << I->first |
| 4903 |
<< "::Instance;\n"; |
| 4904 |
} |
| 4905 |
|
| 4906 |
OS << "static const ParsedAttrInfo *AttrInfoMap[] = {\n"; |
| 4907 |
for (const auto &Attr : Attrs) |
| 4908 |
OS << "&ParsedAttrInfo" << Attr.first << "::Instance,\n"; |
| 4909 |
OS << "};\n\n"; |
| 4910 |
|
| 4911 |
// Generate function for handling attributes with delayed arguments |
| 4912 |
GenerateHandleAttrWithDelayedArgs(Records, OS); |
| 4913 |
|
| 4914 |
// Generate the attribute match rules. |
| 4915 |
emitAttributeMatchRules(PragmaAttributeSupport, OS); |
| 4916 |
|
| 4917 |
OS << "#elif defined(WANT_DECL_MERGE_LOGIC)\n\n"; |
| 4918 |
|
| 4919 |
// Write out the declaration merging check logic. |
| 4920 |
OS << "static bool DiagnoseMutualExclusions(Sema &S, const NamedDecl *D, " |
| 4921 |
<< "const Attr *A) {\n"; |
| 4922 |
OS << DeclMergeChecks; |
| 4923 |
OS << " return true;\n"; |
| 4924 |
OS << "}\n\n"; |
| 4925 |
|
| 4926 |
OS << "#elif defined(WANT_STMT_MERGE_LOGIC)\n\n"; |
| 4927 |
|
| 4928 |
// Write out the statement merging check logic. |
| 4929 |
OS << "static bool DiagnoseMutualExclusions(Sema &S, " |
| 4930 |
<< "const SmallVectorImpl<const Attr *> &C) {\n"; |
| 4931 |
OS << " for (const Attr *A : C) {\n"; |
| 4932 |
OS << StmtMergeChecks; |
| 4933 |
OS << " }\n"; |
| 4934 |
OS << " return true;\n"; |
| 4935 |
OS << "}\n\n"; |
| 4936 |
|
| 4937 |
OS << "#endif\n"; |
| 4938 |
} |
| 4939 |
|
| 4940 |
// Emits the kind list of parsed attributes |
| 4941 |
void EmitClangAttrParsedAttrKinds(const RecordKeeper &Records, |
| 4942 |
raw_ostream &OS) { |
| 4943 |
emitSourceFileHeader("Attribute name matcher", OS, Records); |
| 4944 |
|
| 4945 |
std::vector<StringMatcher::StringPair> GNU, Declspec, Microsoft, CXX11, |
| 4946 |
Keywords, Pragma, C23, HLSLAnnotation; |
| 4947 |
std::set<StringRef> Seen; |
| 4948 |
for (const auto *A : Records.getAllDerivedDefinitions("Attr")) { |
| 4949 |
const Record &Attr = *A; |
| 4950 |
|
| 4951 |
bool SemaHandler = Attr.getValueAsBit("SemaHandler"); |
| 4952 |
bool Ignored = Attr.getValueAsBit("Ignored"); |
| 4953 |
if (SemaHandler || Ignored) { |
| 4954 |
// Attribute spellings can be shared between target-specific attributes, |
| 4955 |
// and can be shared between syntaxes for the same attribute. For |
| 4956 |
// instance, an attribute can be spelled GNU<"interrupt"> for an ARM- |
| 4957 |
// specific attribute, or MSP430-specific attribute. Additionally, an |
| 4958 |
// attribute can be spelled GNU<"dllexport"> and Declspec<"dllexport"> |
| 4959 |
// for the same semantic attribute. Ultimately, we need to map each of |
| 4960 |
// these to a single AttributeCommonInfo::Kind value, but the |
| 4961 |
// StringMatcher class cannot handle duplicate match strings. So we |
| 4962 |
// generate a list of string to match based on the syntax, and emit |
| 4963 |
// multiple string matchers depending on the syntax used. |
| 4964 |
std::string AttrName; |
| 4965 |
if (Attr.isSubClassOf("TargetSpecificAttr") && |
| 4966 |
!Attr.isValueUnset("ParseKind")) { |
| 4967 |
StringRef ParseKind = Attr.getValueAsString("ParseKind"); |
| 4968 |
if (!Seen.insert(ParseKind).second) |
| 4969 |
continue; |
| 4970 |
AttrName = ParseKind.str(); |
| 4971 |
} else { |
| 4972 |
AttrName = NormalizeAttrName(Attr.getName()).str(); |
| 4973 |
} |
| 4974 |
|
| 4975 |
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr); |
| 4976 |
for (const auto &S : Spellings) { |
| 4977 |
StringRef RawSpelling = S.name(); |
| 4978 |
std::vector<StringMatcher::StringPair> *Matches = nullptr; |
| 4979 |
std::string Spelling; |
| 4980 |
StringRef Variety = S.variety(); |
| 4981 |
if (Variety == "CXX11") { |
| 4982 |
Matches = &CXX11; |
| 4983 |
if (!S.nameSpace().empty()) |
| 4984 |
Spelling += S.nameSpace().str() + "::"; |
| 4985 |
} else if (Variety == "C23") { |
| 4986 |
Matches = &C23; |
| 4987 |
if (!S.nameSpace().empty()) |
| 4988 |
Spelling += S.nameSpace().str() + "::"; |
| 4989 |
} else if (Variety == "GNU") { |
| 4990 |
Matches = &GNU; |
| 4991 |
} else if (Variety == "Declspec") { |
| 4992 |
Matches = &Declspec; |
| 4993 |
} else if (Variety == "Microsoft") { |
| 4994 |
Matches = &Microsoft; |
| 4995 |
} else if (Variety == "Keyword") { |
| 4996 |
Matches = &Keywords; |
| 4997 |
} else if (Variety == "Pragma") { |
| 4998 |
Matches = &Pragma; |
| 4999 |
} else if (Variety == "HLSLAnnotation") { |
| 5000 |
Matches = &HLSLAnnotation; |
| 5001 |
if (RawSpelling.compare(RawSpelling.lower()) != 0) |
| 5002 |
PrintError(S.getSpellingRecord().getLoc(), |
| 5003 |
"HLSLAnnotation Attribute must be lower case."); |
| 5004 |
} |
| 5005 |
|
| 5006 |
assert(Matches && "Unsupported spelling variety found"); |
| 5007 |
|
| 5008 |
if (Variety == "GNU") |
| 5009 |
Spelling += NormalizeGNUAttrSpelling(RawSpelling); |
| 5010 |
else |
| 5011 |
Spelling += RawSpelling; |
| 5012 |
|
| 5013 |
if (SemaHandler) |
| 5014 |
Matches->push_back(StringMatcher::StringPair( |
| 5015 |
Spelling, "return AttributeCommonInfo::AT_" + AttrName + ";")); |
| 5016 |
else |
| 5017 |
Matches->push_back(StringMatcher::StringPair( |
| 5018 |
Spelling, "return AttributeCommonInfo::IgnoredAttribute;")); |
| 5019 |
} |
| 5020 |
} |
| 5021 |
} |
| 5022 |
|
| 5023 |
OS << "static AttributeCommonInfo::Kind getAttrKind(StringRef Name, "; |
| 5024 |
OS << "AttributeCommonInfo::Syntax Syntax) {\n"; |
| 5025 |
OS << " if (AttributeCommonInfo::AS_GNU == Syntax) {\n"; |
| 5026 |
StringMatcher("Name", GNU, OS).Emit(); |
| 5027 |
OS << " } else if (AttributeCommonInfo::AS_Declspec == Syntax) {\n"; |
| 5028 |
StringMatcher("Name", Declspec, OS).Emit(); |
| 5029 |
OS << " } else if (AttributeCommonInfo::AS_Microsoft == Syntax) {\n"; |
| 5030 |
StringMatcher("Name", Microsoft, OS).Emit(); |
| 5031 |
OS << " } else if (AttributeCommonInfo::AS_CXX11 == Syntax) {\n"; |
| 5032 |
StringMatcher("Name", CXX11, OS).Emit(); |
| 5033 |
OS << " } else if (AttributeCommonInfo::AS_C23 == Syntax) {\n"; |
| 5034 |
StringMatcher("Name", C23, OS).Emit(); |
| 5035 |
OS << " } else if (AttributeCommonInfo::AS_Keyword == Syntax || "; |
| 5036 |
OS << "AttributeCommonInfo::AS_ContextSensitiveKeyword == Syntax) {\n"; |
| 5037 |
StringMatcher("Name", Keywords, OS).Emit(); |
| 5038 |
OS << " } else if (AttributeCommonInfo::AS_Pragma == Syntax) {\n"; |
| 5039 |
StringMatcher("Name", Pragma, OS).Emit(); |
| 5040 |
OS << " } else if (AttributeCommonInfo::AS_HLSLAnnotation == Syntax) {\n"; |
| 5041 |
StringMatcher("Name", HLSLAnnotation, OS).Emit(); |
| 5042 |
OS << " }\n"; |
| 5043 |
OS << " return AttributeCommonInfo::UnknownAttribute;\n" |
| 5044 |
<< "}\n"; |
| 5045 |
} |
| 5046 |
|
| 5047 |
// Emits Sema calls for type dependent attributes |
| 5048 |
void EmitClangAttrIsTypeDependent(const RecordKeeper &Records, |
| 5049 |
raw_ostream &OS) { |
| 5050 |
emitSourceFileHeader("Attribute is type dependent", OS, Records); |
| 5051 |
|
| 5052 |
OS << "void checkAttrIsTypeDependent(Decl *D, const Attr *A) {\n"; |
| 5053 |
OS << " switch (A->getKind()) {\n"; |
| 5054 |
OS << " default:\n"; |
| 5055 |
OS << " break;\n"; |
| 5056 |
for (const auto *A : Records.getAllDerivedDefinitions("Attr")) { |
| 5057 |
if (A->getValueAsBit("IsTypeDependent")) { |
| 5058 |
OS << " case attr::" << A->getName() << ":\n"; |
| 5059 |
OS << " ActOn" << A->getName() << "Attr(D, A);\n"; |
| 5060 |
OS << " break;\n"; |
| 5061 |
} |
| 5062 |
} |
| 5063 |
OS << " }\n"; |
| 5064 |
OS << "}\n"; |
| 5065 |
} |
| 5066 |
|
| 5067 |
// Emits the code to dump an attribute. |
| 5068 |
void EmitClangAttrTextNodeDump(const RecordKeeper &Records, raw_ostream &OS) { |
| 5069 |
emitSourceFileHeader("Attribute text node dumper", OS, Records); |
| 5070 |
|
| 5071 |
for (const auto *Attr : Records.getAllDerivedDefinitions("Attr")) { |
| 5072 |
const Record &R = *Attr; |
| 5073 |
if (!R.getValueAsBit("ASTNode")) |
| 5074 |
continue; |
| 5075 |
|
| 5076 |
// If the attribute has a semantically-meaningful name (which is determined |
| 5077 |
// by whether there is a Spelling enumeration for it), then write out the |
| 5078 |
// spelling used for the attribute. |
| 5079 |
|
| 5080 |
std::string FunctionContent; |
| 5081 |
raw_string_ostream SS(FunctionContent); |
| 5082 |
|
| 5083 |
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R); |
| 5084 |
if (Spellings.size() > 1 && !SpellingNamesAreCommon(Spellings)) |
| 5085 |
SS << " OS << \" \" << A->getSpelling();\n"; |
| 5086 |
|
| 5087 |
std::vector<const Record *> Args = R.getValueAsListOfDefs("Args"); |
| 5088 |
for (const auto *Arg : Args) |
| 5089 |
createArgument(*Arg, R.getName())->writeDump(SS); |
| 5090 |
|
| 5091 |
if (Attr->getValueAsBit("AcceptsExprPack")) |
| 5092 |
VariadicExprArgument("DelayedArgs", R.getName()).writeDump(OS); |
| 5093 |
|
| 5094 |
if (SS.tell()) { |
| 5095 |
OS << " void Visit" << R.getName() << "Attr(const " << R.getName() |
| 5096 |
<< "Attr *A) {\n"; |
| 5097 |
if (!Args.empty()) |
| 5098 |
OS << " const auto *SA = cast<" << R.getName() |
| 5099 |
<< "Attr>(A); (void)SA;\n"; |
| 5100 |
OS << FunctionContent; |
| 5101 |
OS << " }\n"; |
| 5102 |
} |
| 5103 |
} |
| 5104 |
} |
| 5105 |
|
| 5106 |
void EmitClangAttrNodeTraverse(const RecordKeeper &Records, raw_ostream &OS) { |
| 5107 |
emitSourceFileHeader("Attribute text node traverser", OS, Records); |
| 5108 |
|
| 5109 |
for (const auto *Attr : Records.getAllDerivedDefinitions("Attr")) { |
| 5110 |
const Record &R = *Attr; |
| 5111 |
if (!R.getValueAsBit("ASTNode")) |
| 5112 |
continue; |
| 5113 |
|
| 5114 |
std::string FunctionContent; |
| 5115 |
raw_string_ostream SS(FunctionContent); |
| 5116 |
|
| 5117 |
std::vector<const Record *> Args = R.getValueAsListOfDefs("Args"); |
| 5118 |
for (const auto *Arg : Args) |
| 5119 |
createArgument(*Arg, R.getName())->writeDumpChildren(SS); |
| 5120 |
if (Attr->getValueAsBit("AcceptsExprPack")) |
| 5121 |
VariadicExprArgument("DelayedArgs", R.getName()).writeDumpChildren(SS); |
| 5122 |
if (SS.tell()) { |
| 5123 |
OS << " void Visit" << R.getName() << "Attr(const " << R.getName() |
| 5124 |
<< "Attr *A) {\n"; |
| 5125 |
if (!Args.empty()) |
| 5126 |
OS << " const auto *SA = cast<" << R.getName() |
| 5127 |
<< "Attr>(A); (void)SA;\n"; |
| 5128 |
OS << FunctionContent; |
| 5129 |
OS << " }\n"; |
| 5130 |
} |
| 5131 |
} |
| 5132 |
} |
| 5133 |
|
| 5134 |
void EmitClangAttrParserStringSwitches(const RecordKeeper &Records, |
| 5135 |
raw_ostream &OS) { |
| 5136 |
generateNameToAttrsMap(Records); |
| 5137 |
emitSourceFileHeader("Parser-related llvm::StringSwitch cases", OS, Records); |
| 5138 |
emitClangAttrArgContextList(Records, OS); |
| 5139 |
emitClangAttrIdentifierArgList(Records, OS); |
| 5140 |
emitClangAttrUnevaluatedStringLiteralList(Records, OS); |
| 5141 |
emitClangAttrVariadicIdentifierArgList(Records, OS); |
| 5142 |
emitClangAttrThisIsaIdentifierArgList(Records, OS); |
| 5143 |
emitClangAttrAcceptsExprPack(Records, OS); |
| 5144 |
emitClangAttrTypeArgList(Records, OS); |
| 5145 |
emitClangAttrLateParsedList(Records, OS); |
| 5146 |
emitClangAttrLateParsedExperimentalList(Records, OS); |
| 5147 |
emitClangAttrStrictIdentifierArgList(Records, OS); |
| 5148 |
} |
| 5149 |
|
| 5150 |
void EmitClangAttrSubjectMatchRulesParserStringSwitches( |
| 5151 |
const RecordKeeper &Records, raw_ostream &OS) { |
| 5152 |
getPragmaAttributeSupport(Records).generateParsingHelpers(OS); |
| 5153 |
} |
| 5154 |
|
| 5155 |
void EmitClangAttrDocTable(const RecordKeeper &Records, raw_ostream &OS) { |
| 5156 |
emitSourceFileHeader("Clang attribute documentation", OS, Records); |
| 5157 |
|
| 5158 |
for (const auto *A : Records.getAllDerivedDefinitions("Attr")) { |
| 5159 |
if (!A->getValueAsBit("ASTNode")) |
| 5160 |
continue; |
| 5161 |
std::vector<const Record *> Docs = A->getValueAsListOfDefs("Documentation"); |
| 5162 |
assert(!Docs.empty()); |
| 5163 |
// Only look at the first documentation if there are several. |
| 5164 |
// (Currently there's only one such attr, revisit if this becomes common). |
| 5165 |
StringRef Text = |
| 5166 |
Docs.front()->getValueAsOptionalString("Content").value_or(""); |
| 5167 |
OS << "\nstatic const char AttrDoc_" << A->getName() << "[] = " |
| 5168 |
<< "R\"reST(" << Text.trim() << ")reST\";\n"; |
| 5169 |
} |
| 5170 |
} |
| 5171 |
|
| 5172 |
enum class SpellingKind : size_t { |
| 5173 |
GNU, |
| 5174 |
CXX11, |
| 5175 |
C23, |
| 5176 |
Declspec, |
| 5177 |
Microsoft, |
| 5178 |
Keyword, |
| 5179 |
Pragma, |
| 5180 |
HLSLAnnotation, |
| 5181 |
NumSpellingKinds |
| 5182 |
}; |
| 5183 |
static const size_t NumSpellingKinds = (size_t)SpellingKind::NumSpellingKinds; |
| 5184 |
|
| 5185 |
class SpellingList { |
| 5186 |
std::array<std::vector<std::string>, NumSpellingKinds> Spellings; |
| 5187 |
|
| 5188 |
public: |
| 5189 |
ArrayRef<std::string> operator[](SpellingKind K) const { |
| 5190 |
return Spellings[(size_t)K]; |
| 5191 |
} |
| 5192 |
|
| 5193 |
void add(const Record &Attr, FlattenedSpelling Spelling) { |
| 5194 |
SpellingKind Kind = |
| 5195 |
StringSwitch<SpellingKind>(Spelling.variety()) |
| 5196 |
.Case("GNU", SpellingKind::GNU) |
| 5197 |
.Case("CXX11", SpellingKind::CXX11) |
| 5198 |
.Case("C23", SpellingKind::C23) |
| 5199 |
.Case("Declspec", SpellingKind::Declspec) |
| 5200 |
.Case("Microsoft", SpellingKind::Microsoft) |
| 5201 |
.Case("Keyword", SpellingKind::Keyword) |
| 5202 |
.Case("Pragma", SpellingKind::Pragma) |
| 5203 |
.Case("HLSLAnnotation", SpellingKind::HLSLAnnotation); |
| 5204 |
std::string Name; |
| 5205 |
StringRef NameSpace = Spelling.nameSpace(); |
| 5206 |
if (!NameSpace.empty()) { |
| 5207 |
Name = NameSpace; |
| 5208 |
switch (Kind) { |
| 5209 |
case SpellingKind::CXX11: |
| 5210 |
case SpellingKind::C23: |
| 5211 |
Name += "::"; |
| 5212 |
break; |
| 5213 |
case SpellingKind::Pragma: |
| 5214 |
Name = " "; |
| 5215 |
break; |
| 5216 |
default: |
| 5217 |
PrintFatalError(Attr.getLoc(), "Unexpected namespace in spelling"); |
| 5218 |
} |
| 5219 |
} |
| 5220 |
Name += Spelling.name(); |
| 5221 |
|
| 5222 |
Spellings[(size_t)Kind].push_back(Name); |
| 5223 |
} |
| 5224 |
|
| 5225 |
void merge(const SpellingList &Other) { |
| 5226 |
for (size_t Kind = 0; Kind < NumSpellingKinds; ++Kind) { |
| 5227 |
Spellings[Kind].insert(Spellings[Kind].end(), |
| 5228 |
Other.Spellings[Kind].begin(), |
| 5229 |
Other.Spellings[Kind].end()); |
| 5230 |
} |
| 5231 |
} |
| 5232 |
|
| 5233 |
bool hasSpelling() const { |
| 5234 |
return llvm::any_of(Spellings, [](const auto &L) { return !L.empty(); }); |
| 5235 |
} |
| 5236 |
}; |
| 5237 |
|
| 5238 |
class DocumentationData { |
| 5239 |
public: |
| 5240 |
const Record *Documentation; |
| 5241 |
const Record *Attribute; |
| 5242 |
std::string Heading; |
| 5243 |
SpellingList SupportedSpellings; |
| 5244 |
|
| 5245 |
DocumentationData(const Record &Documentation, const Record &Attribute, |
| 5246 |
std::pair<std::string, SpellingList> HeadingAndSpellings) |
| 5247 |
: Documentation(&Documentation), Attribute(&Attribute), |
| 5248 |
Heading(std::move(HeadingAndSpellings.first)), |
| 5249 |
SupportedSpellings(std::move(HeadingAndSpellings.second)) {} |
| 5250 |
}; |
| 5251 |
|
| 5252 |
static void WriteCategoryHeader(const Record *DocCategory, |
| 5253 |
raw_ostream &OS) { |
| 5254 |
const StringRef Name = DocCategory->getValueAsString("Name"); |
| 5255 |
OS << Name << "\n" << std::string(Name.size(), '=') << "\n"; |
| 5256 |
|
| 5257 |
// If there is content, print that as well. |
| 5258 |
const StringRef ContentStr = DocCategory->getValueAsString("Content"); |
| 5259 |
// Trim leading and trailing newlines and spaces. |
| 5260 |
OS << ContentStr.trim(); |
| 5261 |
|
| 5262 |
OS << "\n\n"; |
| 5263 |
} |
| 5264 |
|
| 5265 |
static std::pair<std::string, SpellingList> |
| 5266 |
GetAttributeHeadingAndSpellings(const Record &Documentation, |
| 5267 |
const Record &Attribute, |
| 5268 |
StringRef Cat) { |
| 5269 |
// FIXME: there is no way to have a per-spelling category for the attribute |
| 5270 |
// documentation. This may not be a limiting factor since the spellings |
| 5271 |
// should generally be consistently applied across the category. |
| 5272 |
|
| 5273 |
if (Cat == "HLSL Semantics") { |
| 5274 |
if (!Attribute.getName().starts_with("HLSL")) |
| 5275 |
PrintFatalError(Attribute.getLoc(), |
| 5276 |
"HLSL semantic attribute name must start with HLSL"); |
| 5277 |
|
| 5278 |
assert(Attribute.getName().size() > 4); |
| 5279 |
std::string Name = Attribute.getName().substr(4).str(); |
| 5280 |
return std::make_pair(std::move(Name), SpellingList()); |
| 5281 |
} |
| 5282 |
|
| 5283 |
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attribute); |
| 5284 |
if (Spellings.empty()) |
| 5285 |
PrintFatalError(Attribute.getLoc(), |
| 5286 |
"Attribute has no supported spellings; cannot be " |
| 5287 |
"documented"); |
| 5288 |
|
| 5289 |
// Determine the heading to be used for this attribute. |
| 5290 |
std::string Heading = Documentation.getValueAsString("Heading").str(); |
| 5291 |
if (Heading.empty()) { |
| 5292 |
// If there's only one spelling, we can simply use that. |
| 5293 |
if (Spellings.size() == 1) |
| 5294 |
Heading = Spellings.begin()->name(); |
| 5295 |
else { |
| 5296 |
std::set<std::string> Uniques; |
| 5297 |
for (const FlattenedSpelling &FS : Spellings) { |
| 5298 |
std::string Spelling = |
| 5299 |
NormalizeNameForSpellingComparison(FS.name()).str(); |
| 5300 |
Uniques.insert(Spelling); |
| 5301 |
} |
| 5302 |
// If the semantic map has only one spelling, that is sufficient for our |
| 5303 |
// needs. |
| 5304 |
if (Uniques.size() == 1) |
| 5305 |
Heading = *Uniques.begin(); |
| 5306 |
// If it's in the undocumented category, just construct a header by |
| 5307 |
// concatenating all the spellings. Might not be great, but better than |
| 5308 |
// nothing. |
| 5309 |
else if (Cat == "Undocumented") |
| 5310 |
Heading = join(Uniques.begin(), Uniques.end(), ", "); |
| 5311 |
} |
| 5312 |
} |
| 5313 |
|
| 5314 |
// If the heading is still empty, it is an error. |
| 5315 |
if (Heading.empty()) |
| 5316 |
PrintFatalError(Attribute.getLoc(), |
| 5317 |
"This attribute requires a heading to be specified"); |
| 5318 |
|
| 5319 |
SpellingList SupportedSpellings; |
| 5320 |
for (const auto &I : Spellings) |
| 5321 |
SupportedSpellings.add(Attribute, I); |
| 5322 |
|
| 5323 |
return std::make_pair(std::move(Heading), std::move(SupportedSpellings)); |
| 5324 |
} |
| 5325 |
|
| 5326 |
static void WriteDocumentation(const RecordKeeper &Records, |
| 5327 |
const DocumentationData &Doc, raw_ostream &OS) { |
| 5328 |
if (StringRef Label = Doc.Documentation->getValueAsString("Label"); |
| 5329 |
!Label.empty()) |
| 5330 |
OS << ".. _" << Label << ":\n\n"; |
| 5331 |
OS << Doc.Heading << "\n" << std::string(Doc.Heading.length(), '-') << "\n"; |
| 5332 |
|
| 5333 |
if (Doc.SupportedSpellings.hasSpelling()) { |
| 5334 |
// List what spelling syntaxes the attribute supports. |
| 5335 |
// Note: "#pragma clang attribute" is handled outside the spelling kinds |
| 5336 |
// loop so it must be last. |
| 5337 |
OS << ".. csv-table:: Supported Syntaxes\n"; |
| 5338 |
OS << " :header: \"GNU\", \"C++11\", \"C23\", \"``__declspec``\","; |
| 5339 |
OS << " \"Keyword\", \"``#pragma``\", \"HLSL Annotation\", \"``#pragma " |
| 5340 |
"clang "; |
| 5341 |
OS << "attribute``\"\n\n \""; |
| 5342 |
for (size_t Kind = 0; Kind != NumSpellingKinds; ++Kind) { |
| 5343 |
SpellingKind K = (SpellingKind)Kind; |
| 5344 |
// TODO: List Microsoft (IDL-style attribute) spellings once we fully |
| 5345 |
// support them. |
| 5346 |
if (K == SpellingKind::Microsoft) |
| 5347 |
continue; |
| 5348 |
|
| 5349 |
bool PrintedAny = false; |
| 5350 |
for (StringRef Spelling : Doc.SupportedSpellings[K]) { |
| 5351 |
if (PrintedAny) |
| 5352 |
OS << " |br| "; |
| 5353 |
OS << "``" << Spelling << "``"; |
| 5354 |
PrintedAny = true; |
| 5355 |
} |
| 5356 |
|
| 5357 |
OS << "\",\""; |
| 5358 |
} |
| 5359 |
|
| 5360 |
if (getPragmaAttributeSupport(Records).isAttributedSupported( |
| 5361 |
*Doc.Attribute)) |
| 5362 |
OS << "Yes"; |
| 5363 |
OS << "\"\n\n"; |
| 5364 |
} |
| 5365 |
|
| 5366 |
// If the attribute is deprecated, print a message about it, and possibly |
| 5367 |
// provide a replacement attribute. |
| 5368 |
if (!Doc.Documentation->isValueUnset("Deprecated")) { |
| 5369 |
OS << "This attribute has been deprecated, and may be removed in a future " |
| 5370 |
<< "version of Clang."; |
| 5371 |
const Record &Deprecated = *Doc.Documentation->getValueAsDef("Deprecated"); |
| 5372 |
const StringRef Replacement = Deprecated.getValueAsString("Replacement"); |
| 5373 |
if (!Replacement.empty()) |
| 5374 |
OS << " This attribute has been superseded by ``" << Replacement |
| 5375 |
<< "``."; |
| 5376 |
OS << "\n\n"; |
| 5377 |
} |
| 5378 |
|
| 5379 |
const StringRef ContentStr = Doc.Documentation->getValueAsString("Content"); |
| 5380 |
// Trim leading and trailing newlines and spaces. |
| 5381 |
OS << ContentStr.trim(); |
| 5382 |
|
| 5383 |
OS << "\n\n\n"; |
| 5384 |
} |
| 5385 |
|
| 5386 |
void EmitClangAttrDocs(const RecordKeeper &Records, raw_ostream &OS) { |
| 5387 |
// Get the documentation introduction paragraph. |
| 5388 |
const Record *Documentation = Records.getDef("GlobalDocumentation"); |
| 5389 |
if (!Documentation) { |
| 5390 |
PrintFatalError("The Documentation top-level definition is missing, " |
| 5391 |
"no documentation will be generated."); |
| 5392 |
return; |
| 5393 |
} |
| 5394 |
|
| 5395 |
OS << Documentation->getValueAsString("Intro") << "\n"; |
| 5396 |
|
| 5397 |
// Gather the Documentation lists from each of the attributes, based on the |
| 5398 |
// category provided. |
| 5399 |
struct CategoryLess { |
| 5400 |
bool operator()(const Record *L, const Record *R) const { |
| 5401 |
return L->getValueAsString("Name") < R->getValueAsString("Name"); |
| 5402 |
} |
| 5403 |
}; |
| 5404 |
|
| 5405 |
std::map<const Record *, std::map<uint32_t, DocumentationData>, CategoryLess> |
| 5406 |
MergedDocs; |
| 5407 |
|
| 5408 |
std::vector<DocumentationData> UndocumentedDocs; |
| 5409 |
const Record *UndocumentedCategory = nullptr; |
| 5410 |
|
| 5411 |
// Collect documentation data, grouping by category and heading. |
| 5412 |
for (const auto *A : Records.getAllDerivedDefinitions("Attr")) { |
| 5413 |
const Record &Attr = *A; |
| 5414 |
std::vector<const Record *> Docs = |
| 5415 |
Attr.getValueAsListOfDefs("Documentation"); |
| 5416 |
|
| 5417 |
for (const auto *D : Docs) { |
| 5418 |
const Record &Doc = *D; |
| 5419 |
const Record *Category = Doc.getValueAsDef("Category"); |
| 5420 |
// If the category is "InternalOnly", then there cannot be any other |
| 5421 |
// documentation categories (otherwise, the attribute would be |
| 5422 |
// emitted into the docs). |
| 5423 |
StringRef Cat = Category->getValueAsString("Name"); |
| 5424 |
if (Cat == "InternalOnly" && Docs.size() > 1) |
| 5425 |
PrintFatalError(Doc.getLoc(), |
| 5426 |
"Attribute is \"InternalOnly\", but has multiple " |
| 5427 |
"documentation categories"); |
| 5428 |
|
| 5429 |
if (Cat == "InternalOnly") |
| 5430 |
continue; |
| 5431 |
|
| 5432 |
// Track the Undocumented category Record for later grouping |
| 5433 |
if (Cat == "Undocumented" && !UndocumentedCategory) |
| 5434 |
UndocumentedCategory = Category; |
| 5435 |
|
| 5436 |
// Generate Heading and Spellings. |
| 5437 |
auto HeadingAndSpellings = |
| 5438 |
GetAttributeHeadingAndSpellings(Doc, Attr, Cat); |
| 5439 |
|
| 5440 |
// Handle Undocumented category separately - no content merging |
| 5441 |
if (Cat == "Undocumented" && UndocumentedCategory) { |
| 5442 |
UndocumentedDocs.push_back( |
| 5443 |
DocumentationData(Doc, Attr, std::move(HeadingAndSpellings))); |
| 5444 |
continue; |
| 5445 |
} |
| 5446 |
|
| 5447 |
auto &CategoryDocs = MergedDocs[Category]; |
| 5448 |
|
| 5449 |
std::string key = Doc.getValueAsString("Content").str(); |
| 5450 |
uint32_t keyHash = llvm::hash_value(key); |
| 5451 |
|
| 5452 |
// If the content already exists, merge the documentation. |
| 5453 |
auto It = CategoryDocs.find(keyHash); |
| 5454 |
if (It != CategoryDocs.end()) { |
| 5455 |
// Merge heading |
| 5456 |
if (It->second.Heading != HeadingAndSpellings.first) |
| 5457 |
It->second.Heading += ", " + HeadingAndSpellings.first; |
| 5458 |
// Merge spellings |
| 5459 |
It->second.SupportedSpellings.merge(HeadingAndSpellings.second); |
| 5460 |
// Merge content |
| 5461 |
It->second.Documentation = &Doc; // Update reference |
| 5462 |
} else { |
| 5463 |
// Create new entry for unique content |
| 5464 |
CategoryDocs.emplace(keyHash, |
| 5465 |
DocumentationData(Doc, Attr, HeadingAndSpellings)); |
| 5466 |
} |
| 5467 |
} |
| 5468 |
} |
| 5469 |
|
| 5470 |
std::map<const Record *, std::vector<DocumentationData>, CategoryLess> |
| 5471 |
SplitDocs; |
| 5472 |
|
| 5473 |
for (auto &CategoryPair : MergedDocs) { |
| 5474 |
|
| 5475 |
std::vector<DocumentationData> MD; |
| 5476 |
for (auto &DocPair : CategoryPair.second) |
| 5477 |
MD.push_back(std::move(DocPair.second)); |
| 5478 |
|
| 5479 |
SplitDocs.emplace(CategoryPair.first, MD); |
| 5480 |
} |
| 5481 |
|
| 5482 |
// Append Undocumented category entries |
| 5483 |
if (!UndocumentedDocs.empty() && UndocumentedCategory) { |
| 5484 |
SplitDocs.emplace(UndocumentedCategory, UndocumentedDocs); |
| 5485 |
} |
| 5486 |
|
| 5487 |
// Having split the attributes out based on what documentation goes where, |
| 5488 |
// we can begin to generate sections of documentation. |
| 5489 |
for (auto &I : SplitDocs) { |
| 5490 |
WriteCategoryHeader(I.first, OS); |
| 5491 |
|
| 5492 |
sort(I.second, |
| 5493 |
[](const DocumentationData &D1, const DocumentationData &D2) { |
| 5494 |
return D1.Heading < D2.Heading; |
| 5495 |
}); |
| 5496 |
|
| 5497 |
// Walk over each of the attributes in the category and write out their |
| 5498 |
// documentation. |
| 5499 |
for (const auto &Doc : I.second) |
| 5500 |
WriteDocumentation(Records, Doc, OS); |
| 5501 |
} |
| 5502 |
} |
| 5503 |
|
| 5504 |
void EmitTestPragmaAttributeSupportedAttributes(const RecordKeeper &Records, |
| 5505 |
raw_ostream &OS) { |
| 5506 |
PragmaClangAttributeSupport Support = getPragmaAttributeSupport(Records); |
| 5507 |
ParsedAttrMap Attrs = getParsedAttrList(Records); |
| 5508 |
OS << "#pragma clang attribute supports the following attributes:\n"; |
| 5509 |
for (const auto &I : Attrs) { |
| 5510 |
if (!Support.isAttributedSupported(*I.second)) |
| 5511 |
continue; |
| 5512 |
OS << I.first; |
| 5513 |
if (I.second->isValueUnset("Subjects")) { |
| 5514 |
OS << " ()\n"; |
| 5515 |
continue; |
| 5516 |
} |
| 5517 |
const Record *SubjectObj = I.second->getValueAsDef("Subjects"); |
| 5518 |
OS << " ("; |
| 5519 |
ListSeparator LS; |
| 5520 |
for (const auto &Subject : |
| 5521 |
enumerate(SubjectObj->getValueAsListOfDefs("Subjects"))) { |
| 5522 |
if (!isSupportedPragmaClangAttributeSubject(*Subject.value())) |
| 5523 |
continue; |
| 5524 |
OS << LS; |
| 5525 |
PragmaClangAttributeSupport::RuleOrAggregateRuleSet &RuleSet = |
| 5526 |
Support.SubjectsToRules.find(Subject.value())->getSecond(); |
| 5527 |
if (RuleSet.isRule()) { |
| 5528 |
OS << RuleSet.getRule().getEnumValueName(); |
| 5529 |
continue; |
| 5530 |
} |
| 5531 |
OS << "("; |
| 5532 |
for (const auto &Rule : enumerate(RuleSet.getAggregateRuleSet())) { |
| 5533 |
if (Rule.index()) |
| 5534 |
OS << ", "; |
| 5535 |
OS << Rule.value().getEnumValueName(); |
| 5536 |
} |
| 5537 |
OS << ")"; |
| 5538 |
} |
| 5539 |
OS << ")\n"; |
| 5540 |
} |
| 5541 |
OS << "End of supported attributes.\n"; |
| 5542 |
} |
| 5543 |
|
| 5544 |
} // end namespace clang |