ParsedAttr.h 46 KB

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