AttributeList.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //===--- AttributeList.cpp --------------------------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the AttributeList class implementation
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Sema/AttributeList.h"
  14. #include "clang/AST/ASTContext.h"
  15. #include "clang/AST/DeclCXX.h"
  16. #include "clang/AST/DeclTemplate.h"
  17. #include "clang/AST/Expr.h"
  18. #include "clang/Basic/IdentifierTable.h"
  19. #include "clang/Sema/SemaInternal.h"
  20. #include "llvm/ADT/SmallString.h"
  21. #include "llvm/ADT/StringSwitch.h"
  22. using namespace clang;
  23. IdentifierLoc *IdentifierLoc::create(ASTContext &Ctx, SourceLocation Loc,
  24. IdentifierInfo *Ident) {
  25. IdentifierLoc *Result = new (Ctx) IdentifierLoc;
  26. Result->Loc = Loc;
  27. Result->Ident = Ident;
  28. return Result;
  29. }
  30. size_t AttributeList::allocated_size() const {
  31. if (IsAvailability) return AttributeFactory::AvailabilityAllocSize;
  32. else if (IsTypeTagForDatatype)
  33. return AttributeFactory::TypeTagForDatatypeAllocSize;
  34. else if (IsProperty)
  35. return AttributeFactory::PropertyAllocSize;
  36. return (sizeof(AttributeList) + NumArgs * sizeof(ArgsUnion));
  37. }
  38. AttributeFactory::AttributeFactory() {
  39. // Go ahead and configure all the inline capacity. This is just a memset.
  40. FreeLists.resize(InlineFreeListsCapacity);
  41. }
  42. AttributeFactory::~AttributeFactory() {}
  43. static size_t getFreeListIndexForSize(size_t size) {
  44. assert(size >= sizeof(AttributeList));
  45. assert((size % sizeof(void*)) == 0);
  46. return ((size - sizeof(AttributeList)) / sizeof(void*));
  47. }
  48. void *AttributeFactory::allocate(size_t size) {
  49. // Check for a previously reclaimed attribute.
  50. size_t index = getFreeListIndexForSize(size);
  51. if (index < FreeLists.size()) {
  52. if (AttributeList *attr = FreeLists[index]) {
  53. FreeLists[index] = attr->NextInPool;
  54. return attr;
  55. }
  56. }
  57. // Otherwise, allocate something new.
  58. return Alloc.Allocate(size, llvm::AlignOf<AttributeFactory>::Alignment);
  59. }
  60. void AttributeFactory::reclaimPool(AttributeList *cur) {
  61. assert(cur && "reclaiming empty pool!");
  62. do {
  63. // Read this here, because we're going to overwrite NextInPool
  64. // when we toss 'cur' into the appropriate queue.
  65. AttributeList *next = cur->NextInPool;
  66. size_t size = cur->allocated_size();
  67. size_t freeListIndex = getFreeListIndexForSize(size);
  68. // Expand FreeLists to the appropriate size, if required.
  69. if (freeListIndex >= FreeLists.size())
  70. FreeLists.resize(freeListIndex+1);
  71. // Add 'cur' to the appropriate free-list.
  72. cur->NextInPool = FreeLists[freeListIndex];
  73. FreeLists[freeListIndex] = cur;
  74. cur = next;
  75. } while (cur);
  76. }
  77. void AttributePool::takePool(AttributeList *pool) {
  78. assert(pool);
  79. // Fast path: this pool is empty.
  80. if (!Head) {
  81. Head = pool;
  82. return;
  83. }
  84. // Reverse the pool onto the current head. This optimizes for the
  85. // pattern of pulling a lot of pools into a single pool.
  86. do {
  87. AttributeList *next = pool->NextInPool;
  88. pool->NextInPool = Head;
  89. Head = pool;
  90. pool = next;
  91. } while (pool);
  92. }
  93. #include "clang/Sema/AttrParsedAttrKinds.inc"
  94. AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name,
  95. const IdentifierInfo *ScopeName,
  96. Syntax SyntaxUsed) {
  97. StringRef AttrName = Name->getName();
  98. SmallString<64> FullName;
  99. if (ScopeName)
  100. FullName += ScopeName->getName();
  101. // Normalize the attribute name, __foo__ becomes foo. This is only allowable
  102. // for GNU attributes.
  103. bool IsGNU = SyntaxUsed == AS_GNU || (SyntaxUsed == AS_CXX11 &&
  104. FullName == "gnu");
  105. if (IsGNU && AttrName.size() >= 4 && AttrName.startswith("__") &&
  106. AttrName.endswith("__"))
  107. AttrName = AttrName.slice(2, AttrName.size() - 2);
  108. // Ensure that in the case of C++11 attributes, we look for '::foo' if it is
  109. // unscoped.
  110. if (ScopeName || SyntaxUsed == AS_CXX11)
  111. FullName += "::";
  112. FullName += AttrName;
  113. // HLSL Change Starts: - support case-insensitive variant
  114. #if 1
  115. AttributeList::Kind Result = ::getAttrKind(FullName, SyntaxUsed);
  116. if (Result == AttributeList::UnknownAttribute) {
  117. std::string lower = FullName.str().lower();
  118. Result = ::getAttrKind(StringRef(lower), SyntaxUsed);
  119. }
  120. return Result;
  121. #else
  122. return ::getAttrKind(FullName, SyntaxUsed);
  123. #endif
  124. // HLSL Change Ends
  125. }
  126. unsigned AttributeList::getAttributeSpellingListIndex() const {
  127. // Both variables will be used in tablegen generated
  128. // attribute spell list index matching code.
  129. StringRef Name = AttrName->getName();
  130. StringRef Scope = ScopeName ? ScopeName->getName() : "";
  131. #include "clang/Sema/AttrSpellingListIndex.inc"
  132. }
  133. struct ParsedAttrInfo {
  134. unsigned NumArgs : 4;
  135. unsigned OptArgs : 4;
  136. unsigned HasCustomParsing : 1;
  137. unsigned IsTargetSpecific : 1;
  138. unsigned IsType : 1;
  139. unsigned IsKnownToGCC : 1;
  140. bool (*DiagAppertainsToDecl)(Sema &S, const AttributeList &Attr,
  141. const Decl *);
  142. bool (*DiagLangOpts)(Sema &S, const AttributeList &Attr);
  143. bool (*ExistsInTarget)(const llvm::Triple &T);
  144. unsigned (*SpellingIndexToSemanticSpelling)(const AttributeList &Attr);
  145. };
  146. namespace {
  147. #include "clang/Sema/AttrParsedAttrImpl.inc"
  148. }
  149. static const ParsedAttrInfo &getInfo(const AttributeList &A) {
  150. return AttrInfoMap[A.getKind()];
  151. }
  152. unsigned AttributeList::getMinArgs() const {
  153. return getInfo(*this).NumArgs;
  154. }
  155. unsigned AttributeList::getMaxArgs() const {
  156. return getMinArgs() + getInfo(*this).OptArgs;
  157. }
  158. bool AttributeList::hasCustomParsing() const {
  159. return getInfo(*this).HasCustomParsing;
  160. }
  161. bool AttributeList::diagnoseAppertainsTo(Sema &S, const Decl *D) const {
  162. return getInfo(*this).DiagAppertainsToDecl(S, *this, D);
  163. }
  164. bool AttributeList::diagnoseLangOpts(Sema &S) const {
  165. return getInfo(*this).DiagLangOpts(S, *this);
  166. }
  167. bool AttributeList::isTargetSpecificAttr() const {
  168. return getInfo(*this).IsTargetSpecific;
  169. }
  170. bool AttributeList::isTypeAttr() const {
  171. return getInfo(*this).IsType;
  172. }
  173. bool AttributeList::existsInTarget(const llvm::Triple &T) const {
  174. return getInfo(*this).ExistsInTarget(T);
  175. }
  176. bool AttributeList::isKnownToGCC() const {
  177. return getInfo(*this).IsKnownToGCC;
  178. }
  179. unsigned AttributeList::getSemanticSpelling() const {
  180. return getInfo(*this).SpellingIndexToSemanticSpelling(*this);
  181. }
  182. bool AttributeList::hasVariadicArg() const {
  183. // If the attribute has the maximum number of optional arguments, we will
  184. // claim that as being variadic. If we someday get an attribute that
  185. // legitimately bumps up against that maximum, we can use another bit to track
  186. // whether it's truly variadic or not.
  187. return getInfo(*this).OptArgs == 15;
  188. }