ParsedAttr.h 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //======- ParsedAttr.h - Parsed attribute sets ------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the ParsedAttr class, which is used to collect
  15. // parsed attributes.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_SEMA_PARSEDATTR_H
  19. #define LLVM_CLANG_SEMA_PARSEDATTR_H
  20. #include "clang/Basic/AttrSubjectMatchRules.h"
  21. #include "clang/Basic/AttributeCommonInfo.h"
  22. #include "clang/Basic/Diagnostic.h"
  23. #include "clang/Basic/SourceLocation.h"
  24. #include "clang/Sema/Ownership.h"
  25. #include "llvm/ADT/PointerUnion.h"
  26. #include "llvm/ADT/SmallVector.h"
  27. #include "llvm/ADT/TinyPtrVector.h"
  28. #include "llvm/Support/Allocator.h"
  29. #include "llvm/Support/Registry.h"
  30. #include "llvm/Support/VersionTuple.h"
  31. #include <cassert>
  32. #include <cstddef>
  33. #include <cstring>
  34. #include <utility>
  35. namespace clang {
  36. class ASTContext;
  37. class Decl;
  38. class Expr;
  39. class IdentifierInfo;
  40. class LangOptions;
  41. class ParsedAttr;
  42. class Sema;
  43. class Stmt;
  44. class TargetInfo;
  45. struct ParsedAttrInfo {
  46. /// Corresponds to the Kind enum.
  47. unsigned AttrKind : 16;
  48. /// The number of required arguments of this attribute.
  49. unsigned NumArgs : 4;
  50. /// The number of optional arguments of this attributes.
  51. unsigned OptArgs : 4;
  52. /// True if the parsing does not match the semantic content.
  53. unsigned HasCustomParsing : 1;
  54. /// True if this attribute is only available for certain targets.
  55. unsigned IsTargetSpecific : 1;
  56. /// True if this attribute applies to types.
  57. unsigned IsType : 1;
  58. /// True if this attribute applies to statements.
  59. unsigned IsStmt : 1;
  60. /// True if this attribute has any spellings that are known to gcc.
  61. unsigned IsKnownToGCC : 1;
  62. /// True if this attribute is supported by #pragma clang attribute.
  63. unsigned IsSupportedByPragmaAttribute : 1;
  64. /// The syntaxes supported by this attribute and how they're spelled.
  65. struct Spelling {
  66. AttributeCommonInfo::Syntax Syntax;
  67. const char *NormalizedFullName;
  68. };
  69. ArrayRef<Spelling> Spellings;
  70. // The names of the known arguments of this attribute.
  71. ArrayRef<const char *> ArgNames;
  72. ParsedAttrInfo(AttributeCommonInfo::Kind AttrKind =
  73. AttributeCommonInfo::NoSemaHandlerAttribute)
  74. : AttrKind(AttrKind), NumArgs(0), OptArgs(0), HasCustomParsing(0),
  75. IsTargetSpecific(0), IsType(0), IsStmt(0), IsKnownToGCC(0),
  76. IsSupportedByPragmaAttribute(0) {}
  77. virtual ~ParsedAttrInfo() = default;
  78. /// Check if this attribute appertains to D, and issue a diagnostic if not.
  79. virtual bool diagAppertainsToDecl(Sema &S, const ParsedAttr &Attr,
  80. const Decl *D) const {
  81. return true;
  82. }
  83. /// Check if this attribute appertains to St, and issue a diagnostic if not.
  84. virtual bool diagAppertainsToStmt(Sema &S, const ParsedAttr &Attr,
  85. const Stmt *St) const {
  86. return true;
  87. }
  88. /// Check if the given attribute is mutually exclusive with other attributes
  89. /// already applied to the given declaration.
  90. virtual bool diagMutualExclusion(Sema &S, const ParsedAttr &A,
  91. const Decl *D) const {
  92. return true;
  93. }
  94. /// Check if this attribute is allowed by the language we are compiling.
  95. virtual bool acceptsLangOpts(const LangOptions &LO) const { return true; }
  96. /// Check if this attribute is allowed when compiling for the given target.
  97. virtual bool existsInTarget(const TargetInfo &Target) const {
  98. return true;
  99. }
  100. /// Convert the spelling index of Attr to a semantic spelling enum value.
  101. virtual unsigned
  102. spellingIndexToSemanticSpelling(const ParsedAttr &Attr) const {
  103. return UINT_MAX;
  104. }
  105. /// Populate Rules with the match rules of this attribute.
  106. virtual void getPragmaAttributeMatchRules(
  107. llvm::SmallVectorImpl<std::pair<attr::SubjectMatchRule, bool>> &Rules,
  108. const LangOptions &LangOpts) const {
  109. }
  110. enum AttrHandling {
  111. NotHandled,
  112. AttributeApplied,
  113. AttributeNotApplied
  114. };
  115. /// If this ParsedAttrInfo knows how to handle this ParsedAttr applied to this
  116. /// Decl then do so and return either AttributeApplied if it was applied or
  117. /// AttributeNotApplied if it wasn't. Otherwise return NotHandled.
  118. virtual AttrHandling handleDeclAttribute(Sema &S, Decl *D,
  119. const ParsedAttr &Attr) const {
  120. return NotHandled;
  121. }
  122. static const ParsedAttrInfo &get(const AttributeCommonInfo &A);
  123. static ArrayRef<const ParsedAttrInfo *> getAllBuiltin();
  124. };
  125. typedef llvm::Registry<ParsedAttrInfo> ParsedAttrInfoRegistry;
  126. /// Represents information about a change in availability for
  127. /// an entity, which is part of the encoding of the 'availability'
  128. /// attribute.
  129. struct AvailabilityChange {
  130. /// The location of the keyword indicating the kind of change.
  131. SourceLocation KeywordLoc;
  132. /// The version number at which the change occurred.
  133. VersionTuple Version;
  134. /// The source range covering the version number.
  135. SourceRange VersionRange;
  136. /// Determine whether this availability change is valid.
  137. bool isValid() const { return !Version.empty(); }
  138. };
  139. namespace detail {
  140. enum AvailabilitySlot {
  141. IntroducedSlot, DeprecatedSlot, ObsoletedSlot, NumAvailabilitySlots
  142. };
  143. /// Describes the trailing object for Availability attribute in ParsedAttr.
  144. struct AvailabilityData {
  145. AvailabilityChange Changes[NumAvailabilitySlots];
  146. SourceLocation StrictLoc;
  147. const Expr *Replacement;
  148. AvailabilityData(const AvailabilityChange &Introduced,
  149. const AvailabilityChange &Deprecated,
  150. const AvailabilityChange &Obsoleted,
  151. SourceLocation Strict, const Expr *ReplaceExpr)
  152. : StrictLoc(Strict), Replacement(ReplaceExpr) {
  153. Changes[IntroducedSlot] = Introduced;
  154. Changes[DeprecatedSlot] = Deprecated;
  155. Changes[ObsoletedSlot] = Obsoleted;
  156. }
  157. };
  158. struct TypeTagForDatatypeData {
  159. ParsedType MatchingCType;
  160. unsigned LayoutCompatible : 1;
  161. unsigned MustBeNull : 1;
  162. };
  163. struct PropertyData {
  164. IdentifierInfo *GetterId, *SetterId;
  165. PropertyData(IdentifierInfo *getterId, IdentifierInfo *setterId)
  166. : GetterId(getterId), SetterId(setterId) {}
  167. };
  168. } // namespace
  169. /// Wraps an identifier and optional source location for the identifier.
  170. struct IdentifierLoc {
  171. SourceLocation Loc;
  172. IdentifierInfo *Ident;
  173. static IdentifierLoc *create(ASTContext &Ctx, SourceLocation Loc,
  174. IdentifierInfo *Ident);
  175. };
  176. /// A union of the various pointer types that can be passed to an
  177. /// ParsedAttr as an argument.
  178. using ArgsUnion = llvm::PointerUnion<Expr *, IdentifierLoc *>;
  179. using ArgsVector = llvm::SmallVector<ArgsUnion, 12U>;
  180. /// ParsedAttr - Represents a syntactic attribute.
  181. ///
  182. /// For a GNU attribute, there are four forms of this construct:
  183. ///
  184. /// 1: __attribute__(( const )). ParmName/Args/NumArgs will all be unused.
  185. /// 2: __attribute__(( mode(byte) )). ParmName used, Args/NumArgs unused.
  186. /// 3: __attribute__(( format(printf, 1, 2) )). ParmName/Args/NumArgs all used.
  187. /// 4: __attribute__(( aligned(16) )). ParmName is unused, Args/Num used.
  188. ///
  189. class ParsedAttr final
  190. : public AttributeCommonInfo,
  191. private llvm::TrailingObjects<
  192. ParsedAttr, ArgsUnion, detail::AvailabilityData,
  193. detail::TypeTagForDatatypeData, ParsedType, detail::PropertyData> {
  194. friend TrailingObjects;
  195. size_t numTrailingObjects(OverloadToken<ArgsUnion>) const { return NumArgs; }
  196. size_t numTrailingObjects(OverloadToken<detail::AvailabilityData>) const {
  197. return IsAvailability;
  198. }
  199. size_t
  200. numTrailingObjects(OverloadToken<detail::TypeTagForDatatypeData>) const {
  201. return IsTypeTagForDatatype;
  202. }
  203. size_t numTrailingObjects(OverloadToken<ParsedType>) const {
  204. return HasParsedType;
  205. }
  206. size_t numTrailingObjects(OverloadToken<detail::PropertyData>) const {
  207. return IsProperty;
  208. }
  209. private:
  210. IdentifierInfo *MacroII = nullptr;
  211. SourceLocation MacroExpansionLoc;
  212. SourceLocation EllipsisLoc;
  213. /// The number of expression arguments this attribute has.
  214. /// The expressions themselves are stored after the object.
  215. unsigned NumArgs : 16;
  216. /// True if already diagnosed as invalid.
  217. mutable unsigned Invalid : 1;
  218. /// True if this attribute was used as a type attribute.
  219. mutable unsigned UsedAsTypeAttr : 1;
  220. /// True if this has the extra information associated with an
  221. /// availability attribute.
  222. unsigned IsAvailability : 1;
  223. /// True if this has extra information associated with a
  224. /// type_tag_for_datatype attribute.
  225. unsigned IsTypeTagForDatatype : 1;
  226. /// True if this has extra information associated with a
  227. /// Microsoft __delcspec(property) attribute.
  228. unsigned IsProperty : 1;
  229. /// True if this has a ParsedType
  230. unsigned HasParsedType : 1;
  231. /// True if the processing cache is valid.
  232. mutable unsigned HasProcessingCache : 1;
  233. /// A cached value.
  234. mutable unsigned ProcessingCache : 8;
  235. /// True if the attribute is specified using '#pragma clang attribute'.
  236. mutable unsigned IsPragmaClangAttribute : 1;
  237. /// The location of the 'unavailable' keyword in an
  238. /// availability attribute.
  239. SourceLocation UnavailableLoc;
  240. const Expr *MessageExpr;
  241. const ParsedAttrInfo &Info;
  242. ArgsUnion *getArgsBuffer() { return getTrailingObjects<ArgsUnion>(); }
  243. ArgsUnion const *getArgsBuffer() const {
  244. return getTrailingObjects<ArgsUnion>();
  245. }
  246. detail::AvailabilityData *getAvailabilityData() {
  247. return getTrailingObjects<detail::AvailabilityData>();
  248. }
  249. const detail::AvailabilityData *getAvailabilityData() const {
  250. return getTrailingObjects<detail::AvailabilityData>();
  251. }
  252. private:
  253. friend class AttributeFactory;
  254. friend class AttributePool;
  255. /// Constructor for attributes with expression arguments.
  256. ParsedAttr(IdentifierInfo *attrName, SourceRange attrRange,
  257. IdentifierInfo *scopeName, SourceLocation scopeLoc,
  258. ArgsUnion *args, unsigned numArgs, Syntax syntaxUsed,
  259. SourceLocation ellipsisLoc)
  260. : AttributeCommonInfo(attrName, scopeName, attrRange, scopeLoc,
  261. syntaxUsed),
  262. EllipsisLoc(ellipsisLoc), NumArgs(numArgs), Invalid(false),
  263. UsedAsTypeAttr(false), IsAvailability(false),
  264. IsTypeTagForDatatype(false), IsProperty(false), HasParsedType(false),
  265. HasProcessingCache(false), IsPragmaClangAttribute(false),
  266. Info(ParsedAttrInfo::get(*this)) {
  267. if (numArgs)
  268. memcpy(getArgsBuffer(), args, numArgs * sizeof(ArgsUnion));
  269. }
  270. /// Constructor for availability attributes.
  271. ParsedAttr(IdentifierInfo *attrName, SourceRange attrRange,
  272. IdentifierInfo *scopeName, SourceLocation scopeLoc,
  273. IdentifierLoc *Parm, const AvailabilityChange &introduced,
  274. const AvailabilityChange &deprecated,
  275. const AvailabilityChange &obsoleted, SourceLocation unavailable,
  276. const Expr *messageExpr, Syntax syntaxUsed, SourceLocation strict,
  277. const Expr *replacementExpr)
  278. : AttributeCommonInfo(attrName, scopeName, attrRange, scopeLoc,
  279. syntaxUsed),
  280. NumArgs(1), Invalid(false), UsedAsTypeAttr(false), IsAvailability(true),
  281. IsTypeTagForDatatype(false), IsProperty(false), HasParsedType(false),
  282. HasProcessingCache(false), IsPragmaClangAttribute(false),
  283. UnavailableLoc(unavailable), MessageExpr(messageExpr),
  284. Info(ParsedAttrInfo::get(*this)) {
  285. ArgsUnion PVal(Parm);
  286. memcpy(getArgsBuffer(), &PVal, sizeof(ArgsUnion));
  287. new (getAvailabilityData()) detail::AvailabilityData(
  288. introduced, deprecated, obsoleted, strict, replacementExpr);
  289. }
  290. /// Constructor for objc_bridge_related attributes.
  291. ParsedAttr(IdentifierInfo *attrName, SourceRange attrRange,
  292. IdentifierInfo *scopeName, SourceLocation scopeLoc,
  293. IdentifierLoc *Parm1, IdentifierLoc *Parm2, IdentifierLoc *Parm3,
  294. Syntax syntaxUsed)
  295. : AttributeCommonInfo(attrName, scopeName, attrRange, scopeLoc,
  296. syntaxUsed),
  297. NumArgs(3), Invalid(false), UsedAsTypeAttr(false),
  298. IsAvailability(false), IsTypeTagForDatatype(false), IsProperty(false),
  299. HasParsedType(false), HasProcessingCache(false),
  300. IsPragmaClangAttribute(false), Info(ParsedAttrInfo::get(*this)) {
  301. ArgsUnion *Args = getArgsBuffer();
  302. Args[0] = Parm1;
  303. Args[1] = Parm2;
  304. Args[2] = Parm3;
  305. }
  306. /// Constructor for type_tag_for_datatype attribute.
  307. ParsedAttr(IdentifierInfo *attrName, SourceRange attrRange,
  308. IdentifierInfo *scopeName, SourceLocation scopeLoc,
  309. IdentifierLoc *ArgKind, ParsedType matchingCType,
  310. bool layoutCompatible, bool mustBeNull, Syntax syntaxUsed)
  311. : AttributeCommonInfo(attrName, scopeName, attrRange, scopeLoc,
  312. syntaxUsed),
  313. NumArgs(1), Invalid(false), UsedAsTypeAttr(false),
  314. IsAvailability(false), IsTypeTagForDatatype(true), IsProperty(false),
  315. HasParsedType(false), HasProcessingCache(false),
  316. IsPragmaClangAttribute(false), Info(ParsedAttrInfo::get(*this)) {
  317. ArgsUnion PVal(ArgKind);
  318. memcpy(getArgsBuffer(), &PVal, sizeof(ArgsUnion));
  319. detail::TypeTagForDatatypeData &ExtraData = getTypeTagForDatatypeDataSlot();
  320. new (&ExtraData.MatchingCType) ParsedType(matchingCType);
  321. ExtraData.LayoutCompatible = layoutCompatible;
  322. ExtraData.MustBeNull = mustBeNull;
  323. }
  324. /// Constructor for attributes with a single type argument.
  325. ParsedAttr(IdentifierInfo *attrName, SourceRange attrRange,
  326. IdentifierInfo *scopeName, SourceLocation scopeLoc,
  327. ParsedType typeArg, Syntax syntaxUsed)
  328. : AttributeCommonInfo(attrName, scopeName, attrRange, scopeLoc,
  329. syntaxUsed),
  330. NumArgs(0), Invalid(false), UsedAsTypeAttr(false),
  331. IsAvailability(false), IsTypeTagForDatatype(false), IsProperty(false),
  332. HasParsedType(true), HasProcessingCache(false),
  333. IsPragmaClangAttribute(false), Info(ParsedAttrInfo::get(*this)) {
  334. new (&getTypeBuffer()) ParsedType(typeArg);
  335. }
  336. /// Constructor for microsoft __declspec(property) attribute.
  337. ParsedAttr(IdentifierInfo *attrName, SourceRange attrRange,
  338. IdentifierInfo *scopeName, SourceLocation scopeLoc,
  339. IdentifierInfo *getterId, IdentifierInfo *setterId,
  340. Syntax syntaxUsed)
  341. : AttributeCommonInfo(attrName, scopeName, attrRange, scopeLoc,
  342. syntaxUsed),
  343. NumArgs(0), Invalid(false), UsedAsTypeAttr(false),
  344. IsAvailability(false), IsTypeTagForDatatype(false), IsProperty(true),
  345. HasParsedType(false), HasProcessingCache(false),
  346. IsPragmaClangAttribute(false), Info(ParsedAttrInfo::get(*this)) {
  347. new (&getPropertyDataBuffer()) detail::PropertyData(getterId, setterId);
  348. }
  349. /// Type tag information is stored immediately following the arguments, if
  350. /// any, at the end of the object. They are mutually exclusive with
  351. /// availability slots.
  352. detail::TypeTagForDatatypeData &getTypeTagForDatatypeDataSlot() {
  353. return *getTrailingObjects<detail::TypeTagForDatatypeData>();
  354. }
  355. const detail::TypeTagForDatatypeData &getTypeTagForDatatypeDataSlot() const {
  356. return *getTrailingObjects<detail::TypeTagForDatatypeData>();
  357. }
  358. /// The type buffer immediately follows the object and are mutually exclusive
  359. /// with arguments.
  360. ParsedType &getTypeBuffer() { return *getTrailingObjects<ParsedType>(); }
  361. const ParsedType &getTypeBuffer() const {
  362. return *getTrailingObjects<ParsedType>();
  363. }
  364. /// The property data immediately follows the object is is mutually exclusive
  365. /// with arguments.
  366. detail::PropertyData &getPropertyDataBuffer() {
  367. assert(IsProperty);
  368. return *getTrailingObjects<detail::PropertyData>();
  369. }
  370. const detail::PropertyData &getPropertyDataBuffer() const {
  371. assert(IsProperty);
  372. return *getTrailingObjects<detail::PropertyData>();
  373. }
  374. size_t allocated_size() const;
  375. public:
  376. ParsedAttr(const ParsedAttr &) = delete;
  377. ParsedAttr(ParsedAttr &&) = delete;
  378. ParsedAttr &operator=(const ParsedAttr &) = delete;
  379. ParsedAttr &operator=(ParsedAttr &&) = delete;
  380. ~ParsedAttr() = delete;
  381. void operator delete(void *) = delete;
  382. bool hasParsedType() const { return HasParsedType; }
  383. /// Is this the Microsoft __declspec(property) attribute?
  384. bool isDeclspecPropertyAttribute() const {
  385. return IsProperty;
  386. }
  387. bool isInvalid() const { return Invalid; }
  388. void setInvalid(bool b = true) const { Invalid = b; }
  389. bool hasProcessingCache() const { return HasProcessingCache; }
  390. unsigned getProcessingCache() const {
  391. assert(hasProcessingCache());
  392. return ProcessingCache;
  393. }
  394. void setProcessingCache(unsigned value) const {
  395. ProcessingCache = value;
  396. HasProcessingCache = true;
  397. }
  398. bool isUsedAsTypeAttr() const { return UsedAsTypeAttr; }
  399. void setUsedAsTypeAttr(bool Used = true) { UsedAsTypeAttr = Used; }
  400. /// True if the attribute is specified using '#pragma clang attribute'.
  401. bool isPragmaClangAttribute() const { return IsPragmaClangAttribute; }
  402. void setIsPragmaClangAttribute() { IsPragmaClangAttribute = true; }
  403. bool isPackExpansion() const { return EllipsisLoc.isValid(); }
  404. SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
  405. /// getNumArgs - Return the number of actual arguments to this attribute.
  406. unsigned getNumArgs() const { return NumArgs; }
  407. /// getArg - Return the specified argument.
  408. ArgsUnion getArg(unsigned Arg) const {
  409. assert(Arg < NumArgs && "Arg access out of range!");
  410. return getArgsBuffer()[Arg];
  411. }
  412. bool isArgExpr(unsigned Arg) const {
  413. return Arg < NumArgs && getArg(Arg).is<Expr*>();
  414. }
  415. Expr *getArgAsExpr(unsigned Arg) const {
  416. return getArg(Arg).get<Expr*>();
  417. }
  418. bool isArgIdent(unsigned Arg) const {
  419. return Arg < NumArgs && getArg(Arg).is<IdentifierLoc*>();
  420. }
  421. IdentifierLoc *getArgAsIdent(unsigned Arg) const {
  422. return getArg(Arg).get<IdentifierLoc*>();
  423. }
  424. const AvailabilityChange &getAvailabilityIntroduced() const {
  425. assert(getParsedKind() == AT_Availability &&
  426. "Not an availability attribute");
  427. return getAvailabilityData()->Changes[detail::IntroducedSlot];
  428. }
  429. const AvailabilityChange &getAvailabilityDeprecated() const {
  430. assert(getParsedKind() == AT_Availability &&
  431. "Not an availability attribute");
  432. return getAvailabilityData()->Changes[detail::DeprecatedSlot];
  433. }
  434. const AvailabilityChange &getAvailabilityObsoleted() const {
  435. assert(getParsedKind() == AT_Availability &&
  436. "Not an availability attribute");
  437. return getAvailabilityData()->Changes[detail::ObsoletedSlot];
  438. }
  439. SourceLocation getStrictLoc() const {
  440. assert(getParsedKind() == AT_Availability &&
  441. "Not an availability attribute");
  442. return getAvailabilityData()->StrictLoc;
  443. }
  444. SourceLocation getUnavailableLoc() const {
  445. assert(getParsedKind() == AT_Availability &&
  446. "Not an availability attribute");
  447. return UnavailableLoc;
  448. }
  449. const Expr * getMessageExpr() const {
  450. assert(getParsedKind() == AT_Availability &&
  451. "Not an availability attribute");
  452. return MessageExpr;
  453. }
  454. const Expr *getReplacementExpr() const {
  455. assert(getParsedKind() == AT_Availability &&
  456. "Not an availability attribute");
  457. return getAvailabilityData()->Replacement;
  458. }
  459. const ParsedType &getMatchingCType() const {
  460. assert(getParsedKind() == AT_TypeTagForDatatype &&
  461. "Not a type_tag_for_datatype attribute");
  462. return getTypeTagForDatatypeDataSlot().MatchingCType;
  463. }
  464. bool getLayoutCompatible() const {
  465. assert(getParsedKind() == AT_TypeTagForDatatype &&
  466. "Not a type_tag_for_datatype attribute");
  467. return getTypeTagForDatatypeDataSlot().LayoutCompatible;
  468. }
  469. bool getMustBeNull() const {
  470. assert(getParsedKind() == AT_TypeTagForDatatype &&
  471. "Not a type_tag_for_datatype attribute");
  472. return getTypeTagForDatatypeDataSlot().MustBeNull;
  473. }
  474. const ParsedType &getTypeArg() const {
  475. assert(HasParsedType && "Not a type attribute");
  476. return getTypeBuffer();
  477. }
  478. IdentifierInfo *getPropertyDataGetter() const {
  479. assert(isDeclspecPropertyAttribute() &&
  480. "Not a __delcspec(property) attribute");
  481. return getPropertyDataBuffer().GetterId;
  482. }
  483. IdentifierInfo *getPropertyDataSetter() const {
  484. assert(isDeclspecPropertyAttribute() &&
  485. "Not a __delcspec(property) attribute");
  486. return getPropertyDataBuffer().SetterId;
  487. }
  488. /// Set the macro identifier info object that this parsed attribute was
  489. /// declared in if it was declared in a macro. Also set the expansion location
  490. /// of the macro.
  491. void setMacroIdentifier(IdentifierInfo *MacroName, SourceLocation Loc) {
  492. MacroII = MacroName;
  493. MacroExpansionLoc = Loc;
  494. }
  495. /// Returns true if this attribute was declared in a macro.
  496. bool hasMacroIdentifier() const { return MacroII != nullptr; }
  497. /// Return the macro identifier if this attribute was declared in a macro.
  498. /// nullptr is returned if it was not declared in a macro.
  499. IdentifierInfo *getMacroIdentifier() const { return MacroII; }
  500. SourceLocation getMacroExpansionLoc() const {
  501. assert(hasMacroIdentifier() && "Can only get the macro expansion location "
  502. "if this attribute has a macro identifier.");
  503. return MacroExpansionLoc;
  504. }
  505. /// Check if the attribute has exactly as many args as Num. May output an
  506. /// error. Returns false if a diagnostic is produced.
  507. bool checkExactlyNumArgs(class Sema &S, unsigned Num) const;
  508. /// Check if the attribute has at least as many args as Num. May output an
  509. /// error. Returns false if a diagnostic is produced.
  510. bool checkAtLeastNumArgs(class Sema &S, unsigned Num) const;
  511. /// Check if the attribute has at most as many args as Num. May output an
  512. /// error. Returns false if a diagnostic is produced.
  513. bool checkAtMostNumArgs(class Sema &S, unsigned Num) const;
  514. bool isTargetSpecificAttr() const;
  515. bool isTypeAttr() const;
  516. bool isStmtAttr() const;
  517. bool hasCustomParsing() const;
  518. unsigned getMinArgs() const;
  519. unsigned getMaxArgs() const;
  520. bool hasVariadicArg() const;
  521. bool diagnoseAppertainsTo(class Sema &S, const Decl *D) const;
  522. bool diagnoseAppertainsTo(class Sema &S, const Stmt *St) const;
  523. bool diagnoseMutualExclusion(class Sema &S, const Decl *D) const;
  524. // This function stub exists for parity with the declaration checking code so
  525. // that checkCommonAttributeFeatures() can work generically on declarations
  526. // or statements.
  527. bool diagnoseMutualExclusion(class Sema &S, const Stmt *St) const {
  528. return true;
  529. }
  530. bool appliesToDecl(const Decl *D, attr::SubjectMatchRule MatchRule) const;
  531. void getMatchRules(const LangOptions &LangOpts,
  532. SmallVectorImpl<std::pair<attr::SubjectMatchRule, bool>>
  533. &MatchRules) const;
  534. bool diagnoseLangOpts(class Sema &S) const;
  535. bool existsInTarget(const TargetInfo &Target) const;
  536. bool isKnownToGCC() const;
  537. bool isSupportedByPragmaAttribute() const;
  538. /// If the parsed attribute has a semantic equivalent, and it would
  539. /// have a semantic Spelling enumeration (due to having semantically-distinct
  540. /// spelling variations), return the value of that semantic spelling. If the
  541. /// parsed attribute does not have a semantic equivalent, or would not have
  542. /// a Spelling enumeration, the value UINT_MAX is returned.
  543. unsigned getSemanticSpelling() const;
  544. /// If this is an OpenCL address space attribute, returns its representation
  545. /// in LangAS, otherwise returns default address space.
  546. LangAS asOpenCLLangAS() const {
  547. switch (getParsedKind()) {
  548. case ParsedAttr::AT_OpenCLConstantAddressSpace:
  549. return LangAS::opencl_constant;
  550. case ParsedAttr::AT_OpenCLGlobalAddressSpace:
  551. return LangAS::opencl_global;
  552. case ParsedAttr::AT_OpenCLGlobalDeviceAddressSpace:
  553. return LangAS::opencl_global_device;
  554. case ParsedAttr::AT_OpenCLGlobalHostAddressSpace:
  555. return LangAS::opencl_global_host;
  556. case ParsedAttr::AT_OpenCLLocalAddressSpace:
  557. return LangAS::opencl_local;
  558. case ParsedAttr::AT_OpenCLPrivateAddressSpace:
  559. return LangAS::opencl_private;
  560. case ParsedAttr::AT_OpenCLGenericAddressSpace:
  561. return LangAS::opencl_generic;
  562. default:
  563. return LangAS::Default;
  564. }
  565. }
  566. /// If this is an OpenCL address space attribute, returns its SYCL
  567. /// representation in LangAS, otherwise returns default address space.
  568. LangAS asSYCLLangAS() const {
  569. switch (getKind()) {
  570. case ParsedAttr::AT_OpenCLGlobalAddressSpace:
  571. return LangAS::sycl_global;
  572. case ParsedAttr::AT_OpenCLGlobalDeviceAddressSpace:
  573. return LangAS::sycl_global_device;
  574. case ParsedAttr::AT_OpenCLGlobalHostAddressSpace:
  575. return LangAS::sycl_global_host;
  576. case ParsedAttr::AT_OpenCLLocalAddressSpace:
  577. return LangAS::sycl_local;
  578. case ParsedAttr::AT_OpenCLPrivateAddressSpace:
  579. return LangAS::sycl_private;
  580. case ParsedAttr::AT_OpenCLGenericAddressSpace:
  581. default:
  582. return LangAS::Default;
  583. }
  584. }
  585. AttributeCommonInfo::Kind getKind() const {
  586. return AttributeCommonInfo::Kind(Info.AttrKind);
  587. }
  588. const ParsedAttrInfo &getInfo() const { return Info; }
  589. };
  590. class AttributePool;
  591. /// A factory, from which one makes pools, from which one creates
  592. /// individual attributes which are deallocated with the pool.
  593. ///
  594. /// Note that it's tolerably cheap to create and destroy one of
  595. /// these as long as you don't actually allocate anything in it.
  596. class AttributeFactory {
  597. public:
  598. enum {
  599. AvailabilityAllocSize =
  600. ParsedAttr::totalSizeToAlloc<ArgsUnion, detail::AvailabilityData,
  601. detail::TypeTagForDatatypeData, ParsedType,
  602. detail::PropertyData>(1, 1, 0, 0, 0),
  603. TypeTagForDatatypeAllocSize =
  604. ParsedAttr::totalSizeToAlloc<ArgsUnion, detail::AvailabilityData,
  605. detail::TypeTagForDatatypeData, ParsedType,
  606. detail::PropertyData>(1, 0, 1, 0, 0),
  607. PropertyAllocSize =
  608. ParsedAttr::totalSizeToAlloc<ArgsUnion, detail::AvailabilityData,
  609. detail::TypeTagForDatatypeData, ParsedType,
  610. detail::PropertyData>(0, 0, 0, 0, 1),
  611. };
  612. private:
  613. enum {
  614. /// The number of free lists we want to be sure to support
  615. /// inline. This is just enough that availability attributes
  616. /// don't surpass it. It's actually very unlikely we'll see an
  617. /// attribute that needs more than that; on x86-64 you'd need 10
  618. /// expression arguments, and on i386 you'd need 19.
  619. InlineFreeListsCapacity =
  620. 1 + (AvailabilityAllocSize - sizeof(ParsedAttr)) / sizeof(void *)
  621. };
  622. llvm::BumpPtrAllocator Alloc;
  623. /// Free lists. The index is determined by the following formula:
  624. /// (size - sizeof(ParsedAttr)) / sizeof(void*)
  625. SmallVector<SmallVector<ParsedAttr *, 8>, InlineFreeListsCapacity> FreeLists;
  626. // The following are the private interface used by AttributePool.
  627. friend class AttributePool;
  628. /// Allocate an attribute of the given size.
  629. void *allocate(size_t size);
  630. void deallocate(ParsedAttr *AL);
  631. /// Reclaim all the attributes in the given pool chain, which is
  632. /// non-empty. Note that the current implementation is safe
  633. /// against reclaiming things which were not actually allocated
  634. /// with the allocator, although of course it's important to make
  635. /// sure that their allocator lives at least as long as this one.
  636. void reclaimPool(AttributePool &head);
  637. public:
  638. AttributeFactory();
  639. ~AttributeFactory();
  640. };
  641. class AttributePool {
  642. friend class AttributeFactory;
  643. friend class ParsedAttributes;
  644. AttributeFactory &Factory;
  645. llvm::TinyPtrVector<ParsedAttr *> Attrs;
  646. void *allocate(size_t size) {
  647. return Factory.allocate(size);
  648. }
  649. ParsedAttr *add(ParsedAttr *attr) {
  650. Attrs.push_back(attr);
  651. return attr;
  652. }
  653. void remove(ParsedAttr *attr) {
  654. assert(llvm::is_contained(Attrs, attr) &&
  655. "Can't take attribute from a pool that doesn't own it!");
  656. Attrs.erase(llvm::find(Attrs, attr));
  657. }
  658. void takePool(AttributePool &pool);
  659. public:
  660. /// Create a new pool for a factory.
  661. AttributePool(AttributeFactory &factory) : Factory(factory) {}
  662. AttributePool(const AttributePool &) = delete;
  663. ~AttributePool() { Factory.reclaimPool(*this); }
  664. /// Move the given pool's allocations to this pool.
  665. AttributePool(AttributePool &&pool) = default;
  666. AttributeFactory &getFactory() const { return Factory; }
  667. void clear() {
  668. Factory.reclaimPool(*this);
  669. Attrs.clear();
  670. }
  671. /// Take the given pool's allocations and add them to this pool.
  672. void takeAllFrom(AttributePool &pool) {
  673. takePool(pool);
  674. pool.Attrs.clear();
  675. }
  676. ParsedAttr *create(IdentifierInfo *attrName, SourceRange attrRange,
  677. IdentifierInfo *scopeName, SourceLocation scopeLoc,
  678. ArgsUnion *args, unsigned numArgs,
  679. ParsedAttr::Syntax syntax,
  680. SourceLocation ellipsisLoc = SourceLocation()) {
  681. size_t temp =
  682. ParsedAttr::totalSizeToAlloc<ArgsUnion, detail::AvailabilityData,
  683. detail::TypeTagForDatatypeData, ParsedType,
  684. detail::PropertyData>(numArgs, 0, 0, 0, 0);
  685. (void)temp;
  686. void *memory = allocate(
  687. ParsedAttr::totalSizeToAlloc<ArgsUnion, detail::AvailabilityData,
  688. detail::TypeTagForDatatypeData, ParsedType,
  689. detail::PropertyData>(numArgs, 0, 0, 0,
  690. 0));
  691. return add(new (memory) ParsedAttr(attrName, attrRange, scopeName, scopeLoc,
  692. args, numArgs, syntax, ellipsisLoc));
  693. }
  694. ParsedAttr *create(IdentifierInfo *attrName, SourceRange attrRange,
  695. IdentifierInfo *scopeName, SourceLocation scopeLoc,
  696. IdentifierLoc *Param, const AvailabilityChange &introduced,
  697. const AvailabilityChange &deprecated,
  698. const AvailabilityChange &obsoleted,
  699. SourceLocation unavailable, const Expr *MessageExpr,
  700. ParsedAttr::Syntax syntax, SourceLocation strict,
  701. const Expr *ReplacementExpr) {
  702. void *memory = allocate(AttributeFactory::AvailabilityAllocSize);
  703. return add(new (memory) ParsedAttr(
  704. attrName, attrRange, scopeName, scopeLoc, Param, introduced, deprecated,
  705. obsoleted, unavailable, MessageExpr, syntax, strict, ReplacementExpr));
  706. }
  707. ParsedAttr *create(IdentifierInfo *attrName, SourceRange attrRange,
  708. IdentifierInfo *scopeName, SourceLocation scopeLoc,
  709. IdentifierLoc *Param1, IdentifierLoc *Param2,
  710. IdentifierLoc *Param3, ParsedAttr::Syntax syntax) {
  711. void *memory = allocate(
  712. ParsedAttr::totalSizeToAlloc<ArgsUnion, detail::AvailabilityData,
  713. detail::TypeTagForDatatypeData, ParsedType,
  714. detail::PropertyData>(3, 0, 0, 0, 0));
  715. return add(new (memory) ParsedAttr(attrName, attrRange, scopeName, scopeLoc,
  716. Param1, Param2, Param3, syntax));
  717. }
  718. ParsedAttr *
  719. createTypeTagForDatatype(IdentifierInfo *attrName, SourceRange attrRange,
  720. IdentifierInfo *scopeName, SourceLocation scopeLoc,
  721. IdentifierLoc *argumentKind,
  722. ParsedType matchingCType, bool layoutCompatible,
  723. bool mustBeNull, ParsedAttr::Syntax syntax) {
  724. void *memory = allocate(AttributeFactory::TypeTagForDatatypeAllocSize);
  725. return add(new (memory) ParsedAttr(attrName, attrRange, scopeName, scopeLoc,
  726. argumentKind, matchingCType,
  727. layoutCompatible, mustBeNull, syntax));
  728. }
  729. ParsedAttr *createTypeAttribute(IdentifierInfo *attrName,
  730. SourceRange attrRange,
  731. IdentifierInfo *scopeName,
  732. SourceLocation scopeLoc, ParsedType typeArg,
  733. ParsedAttr::Syntax syntaxUsed) {
  734. void *memory = allocate(
  735. ParsedAttr::totalSizeToAlloc<ArgsUnion, detail::AvailabilityData,
  736. detail::TypeTagForDatatypeData, ParsedType,
  737. detail::PropertyData>(0, 0, 0, 1, 0));
  738. return add(new (memory) ParsedAttr(attrName, attrRange, scopeName, scopeLoc,
  739. typeArg, syntaxUsed));
  740. }
  741. ParsedAttr *
  742. createPropertyAttribute(IdentifierInfo *attrName, SourceRange attrRange,
  743. IdentifierInfo *scopeName, SourceLocation scopeLoc,
  744. IdentifierInfo *getterId, IdentifierInfo *setterId,
  745. ParsedAttr::Syntax syntaxUsed) {
  746. void *memory = allocate(AttributeFactory::PropertyAllocSize);
  747. return add(new (memory) ParsedAttr(attrName, attrRange, scopeName, scopeLoc,
  748. getterId, setterId, syntaxUsed));
  749. }
  750. };
  751. class ParsedAttributesView {
  752. using VecTy = llvm::TinyPtrVector<ParsedAttr *>;
  753. using SizeType = decltype(std::declval<VecTy>().size());
  754. public:
  755. bool empty() const { return AttrList.empty(); }
  756. SizeType size() const { return AttrList.size(); }
  757. ParsedAttr &operator[](SizeType pos) { return *AttrList[pos]; }
  758. const ParsedAttr &operator[](SizeType pos) const { return *AttrList[pos]; }
  759. void addAtEnd(ParsedAttr *newAttr) {
  760. assert(newAttr);
  761. AttrList.push_back(newAttr);
  762. }
  763. void remove(ParsedAttr *ToBeRemoved) {
  764. assert(is_contained(AttrList, ToBeRemoved) &&
  765. "Cannot remove attribute that isn't in the list");
  766. AttrList.erase(llvm::find(AttrList, ToBeRemoved));
  767. }
  768. void clearListOnly() { AttrList.clear(); }
  769. struct iterator : llvm::iterator_adaptor_base<iterator, VecTy::iterator,
  770. std::random_access_iterator_tag,
  771. ParsedAttr> {
  772. iterator() : iterator_adaptor_base(nullptr) {}
  773. iterator(VecTy::iterator I) : iterator_adaptor_base(I) {}
  774. reference operator*() const { return **I; }
  775. friend class ParsedAttributesView;
  776. };
  777. struct const_iterator
  778. : llvm::iterator_adaptor_base<const_iterator, VecTy::const_iterator,
  779. std::random_access_iterator_tag,
  780. ParsedAttr> {
  781. const_iterator() : iterator_adaptor_base(nullptr) {}
  782. const_iterator(VecTy::const_iterator I) : iterator_adaptor_base(I) {}
  783. reference operator*() const { return **I; }
  784. friend class ParsedAttributesView;
  785. };
  786. void addAll(iterator B, iterator E) {
  787. AttrList.insert(AttrList.begin(), B.I, E.I);
  788. }
  789. void addAll(const_iterator B, const_iterator E) {
  790. AttrList.insert(AttrList.begin(), B.I, E.I);
  791. }
  792. void addAllAtEnd(iterator B, iterator E) {
  793. AttrList.insert(AttrList.end(), B.I, E.I);
  794. }
  795. void addAllAtEnd(const_iterator B, const_iterator E) {
  796. AttrList.insert(AttrList.end(), B.I, E.I);
  797. }
  798. iterator begin() { return iterator(AttrList.begin()); }
  799. const_iterator begin() const { return const_iterator(AttrList.begin()); }
  800. iterator end() { return iterator(AttrList.end()); }
  801. const_iterator end() const { return const_iterator(AttrList.end()); }
  802. ParsedAttr &front() {
  803. assert(!empty());
  804. return *AttrList.front();
  805. }
  806. const ParsedAttr &front() const {
  807. assert(!empty());
  808. return *AttrList.front();
  809. }
  810. ParsedAttr &back() {
  811. assert(!empty());
  812. return *AttrList.back();
  813. }
  814. const ParsedAttr &back() const {
  815. assert(!empty());
  816. return *AttrList.back();
  817. }
  818. bool hasAttribute(ParsedAttr::Kind K) const {
  819. return llvm::any_of(AttrList, [K](const ParsedAttr *AL) {
  820. return AL->getParsedKind() == K;
  821. });
  822. }
  823. private:
  824. VecTy AttrList;
  825. };
  826. /// ParsedAttributes - A collection of parsed attributes. Currently
  827. /// we don't differentiate between the various attribute syntaxes,
  828. /// which is basically silly.
  829. ///
  830. /// Right now this is a very lightweight container, but the expectation
  831. /// is that this will become significantly more serious.
  832. class ParsedAttributes : public ParsedAttributesView {
  833. public:
  834. ParsedAttributes(AttributeFactory &factory) : pool(factory) {}
  835. ParsedAttributes(const ParsedAttributes &) = delete;
  836. AttributePool &getPool() const { return pool; }
  837. void takeAllFrom(ParsedAttributes &attrs) {
  838. addAll(attrs.begin(), attrs.end());
  839. attrs.clearListOnly();
  840. pool.takeAllFrom(attrs.pool);
  841. }
  842. void takeOneFrom(ParsedAttributes &Attrs, ParsedAttr *PA) {
  843. Attrs.getPool().remove(PA);
  844. Attrs.remove(PA);
  845. getPool().add(PA);
  846. addAtEnd(PA);
  847. }
  848. void clear() {
  849. clearListOnly();
  850. pool.clear();
  851. }
  852. /// Add attribute with expression arguments.
  853. ParsedAttr *addNew(IdentifierInfo *attrName, SourceRange attrRange,
  854. IdentifierInfo *scopeName, SourceLocation scopeLoc,
  855. ArgsUnion *args, unsigned numArgs,
  856. ParsedAttr::Syntax syntax,
  857. SourceLocation ellipsisLoc = SourceLocation()) {
  858. ParsedAttr *attr = pool.create(attrName, attrRange, scopeName, scopeLoc,
  859. args, numArgs, syntax, ellipsisLoc);
  860. addAtEnd(attr);
  861. return attr;
  862. }
  863. /// Add availability attribute.
  864. ParsedAttr *addNew(IdentifierInfo *attrName, SourceRange attrRange,
  865. IdentifierInfo *scopeName, SourceLocation scopeLoc,
  866. IdentifierLoc *Param, const AvailabilityChange &introduced,
  867. const AvailabilityChange &deprecated,
  868. const AvailabilityChange &obsoleted,
  869. SourceLocation unavailable, const Expr *MessageExpr,
  870. ParsedAttr::Syntax syntax, SourceLocation strict,
  871. const Expr *ReplacementExpr) {
  872. ParsedAttr *attr = pool.create(
  873. attrName, attrRange, scopeName, scopeLoc, Param, introduced, deprecated,
  874. obsoleted, unavailable, MessageExpr, syntax, strict, ReplacementExpr);
  875. addAtEnd(attr);
  876. return attr;
  877. }
  878. /// Add objc_bridge_related attribute.
  879. ParsedAttr *addNew(IdentifierInfo *attrName, SourceRange attrRange,
  880. IdentifierInfo *scopeName, SourceLocation scopeLoc,
  881. IdentifierLoc *Param1, IdentifierLoc *Param2,
  882. IdentifierLoc *Param3, ParsedAttr::Syntax syntax) {
  883. ParsedAttr *attr = pool.create(attrName, attrRange, scopeName, scopeLoc,
  884. Param1, Param2, Param3, syntax);
  885. addAtEnd(attr);
  886. return attr;
  887. }
  888. /// Add type_tag_for_datatype attribute.
  889. ParsedAttr *
  890. addNewTypeTagForDatatype(IdentifierInfo *attrName, SourceRange attrRange,
  891. IdentifierInfo *scopeName, SourceLocation scopeLoc,
  892. IdentifierLoc *argumentKind,
  893. ParsedType matchingCType, bool layoutCompatible,
  894. bool mustBeNull, ParsedAttr::Syntax syntax) {
  895. ParsedAttr *attr = pool.createTypeTagForDatatype(
  896. attrName, attrRange, scopeName, scopeLoc, argumentKind, matchingCType,
  897. layoutCompatible, mustBeNull, syntax);
  898. addAtEnd(attr);
  899. return attr;
  900. }
  901. /// Add an attribute with a single type argument.
  902. ParsedAttr *addNewTypeAttr(IdentifierInfo *attrName, SourceRange attrRange,
  903. IdentifierInfo *scopeName, SourceLocation scopeLoc,
  904. ParsedType typeArg,
  905. ParsedAttr::Syntax syntaxUsed) {
  906. ParsedAttr *attr = pool.createTypeAttribute(attrName, attrRange, scopeName,
  907. scopeLoc, typeArg, syntaxUsed);
  908. addAtEnd(attr);
  909. return attr;
  910. }
  911. /// Add microsoft __delspec(property) attribute.
  912. ParsedAttr *
  913. addNewPropertyAttr(IdentifierInfo *attrName, SourceRange attrRange,
  914. IdentifierInfo *scopeName, SourceLocation scopeLoc,
  915. IdentifierInfo *getterId, IdentifierInfo *setterId,
  916. ParsedAttr::Syntax syntaxUsed) {
  917. ParsedAttr *attr =
  918. pool.createPropertyAttribute(attrName, attrRange, scopeName, scopeLoc,
  919. getterId, setterId, syntaxUsed);
  920. addAtEnd(attr);
  921. return attr;
  922. }
  923. private:
  924. mutable AttributePool pool;
  925. };
  926. struct ParsedAttributesWithRange : ParsedAttributes {
  927. ParsedAttributesWithRange(AttributeFactory &factory)
  928. : ParsedAttributes(factory) {}
  929. void clear() {
  930. ParsedAttributes::clear();
  931. Range = SourceRange();
  932. }
  933. SourceRange Range;
  934. };
  935. struct ParsedAttributesViewWithRange : ParsedAttributesView {
  936. ParsedAttributesViewWithRange() {}
  937. void clearListOnly() {
  938. ParsedAttributesView::clearListOnly();
  939. Range = SourceRange();
  940. }
  941. SourceRange Range;
  942. };
  943. /// These constants match the enumerated choices of
  944. /// err_attribute_argument_n_type and err_attribute_argument_type.
  945. enum AttributeArgumentNType {
  946. AANT_ArgumentIntOrBool,
  947. AANT_ArgumentIntegerConstant,
  948. AANT_ArgumentString,
  949. AANT_ArgumentIdentifier,
  950. AANT_ArgumentConstantExpr,
  951. AANT_ArgumentBuiltinFunction,
  952. };
  953. /// These constants match the enumerated choices of
  954. /// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
  955. enum AttributeDeclKind {
  956. ExpectedFunction,
  957. ExpectedUnion,
  958. ExpectedVariableOrFunction,
  959. ExpectedFunctionOrMethod,
  960. ExpectedFunctionMethodOrBlock,
  961. ExpectedFunctionMethodOrParameter,
  962. ExpectedVariable,
  963. ExpectedVariableOrField,
  964. ExpectedVariableFieldOrTag,
  965. ExpectedTypeOrNamespace,
  966. ExpectedFunctionVariableOrClass,
  967. ExpectedKernelFunction,
  968. ExpectedFunctionWithProtoType,
  969. };
  970. inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB,
  971. const ParsedAttr &At) {
  972. DB.AddTaggedVal(reinterpret_cast<uint64_t>(At.getAttrName()),
  973. DiagnosticsEngine::ak_identifierinfo);
  974. return DB;
  975. }
  976. inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB,
  977. const ParsedAttr *At) {
  978. DB.AddTaggedVal(reinterpret_cast<uint64_t>(At->getAttrName()),
  979. DiagnosticsEngine::ak_identifierinfo);
  980. return DB;
  981. }
  982. /// AttributeCommonInfo has a non-explicit constructor which takes an
  983. /// SourceRange as its only argument, this constructor has many uses so making
  984. /// it explicit is hard. This constructor causes ambiguity with
  985. /// DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, SourceRange R).
  986. /// We use SFINAE to disable any conversion and remove any ambiguity.
  987. template <typename ACI,
  988. typename std::enable_if_t<
  989. std::is_same<ACI, AttributeCommonInfo>::value, int> = 0>
  990. inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB,
  991. const ACI &CI) {
  992. DB.AddTaggedVal(reinterpret_cast<uint64_t>(CI.getAttrName()),
  993. DiagnosticsEngine::ak_identifierinfo);
  994. return DB;
  995. }
  996. template <typename ACI,
  997. typename std::enable_if_t<
  998. std::is_same<ACI, AttributeCommonInfo>::value, int> = 0>
  999. inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB,
  1000. const ACI* CI) {
  1001. DB.AddTaggedVal(reinterpret_cast<uint64_t>(CI->getAttrName()),
  1002. DiagnosticsEngine::ak_identifierinfo);
  1003. return DB;
  1004. }
  1005. } // namespace clang
  1006. #endif // LLVM_CLANG_SEMA_PARSEDATTR_H
  1007. #ifdef __GNUC__
  1008. #pragma GCC diagnostic pop
  1009. #endif