AttributeList.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/Expr.h"
  16. #include "clang/Basic/IdentifierTable.h"
  17. #include "llvm/ADT/StringSwitch.h"
  18. #include "llvm/ADT/SmallString.h"
  19. using namespace clang;
  20. size_t AttributeList::allocated_size() const {
  21. if (IsAvailability) return AttributeFactory::AvailabilityAllocSize;
  22. return (sizeof(AttributeList) + NumArgs * sizeof(Expr*));
  23. }
  24. AttributeFactory::AttributeFactory() {
  25. // Go ahead and configure all the inline capacity. This is just a memset.
  26. FreeLists.resize(InlineFreeListsCapacity);
  27. }
  28. AttributeFactory::~AttributeFactory() {}
  29. static size_t getFreeListIndexForSize(size_t size) {
  30. assert(size >= sizeof(AttributeList));
  31. assert((size % sizeof(void*)) == 0);
  32. return ((size - sizeof(AttributeList)) / sizeof(void*));
  33. }
  34. void *AttributeFactory::allocate(size_t size) {
  35. // Check for a previously reclaimed attribute.
  36. size_t index = getFreeListIndexForSize(size);
  37. if (index < FreeLists.size()) {
  38. if (AttributeList *attr = FreeLists[index]) {
  39. FreeLists[index] = attr->NextInPool;
  40. return attr;
  41. }
  42. }
  43. // Otherwise, allocate something new.
  44. return Alloc.Allocate(size, llvm::AlignOf<AttributeFactory>::Alignment);
  45. }
  46. void AttributeFactory::reclaimPool(AttributeList *cur) {
  47. assert(cur && "reclaiming empty pool!");
  48. do {
  49. // Read this here, because we're going to overwrite NextInPool
  50. // when we toss 'cur' into the appropriate queue.
  51. AttributeList *next = cur->NextInPool;
  52. size_t size = cur->allocated_size();
  53. size_t freeListIndex = getFreeListIndexForSize(size);
  54. // Expand FreeLists to the appropriate size, if required.
  55. if (freeListIndex >= FreeLists.size())
  56. FreeLists.resize(freeListIndex+1);
  57. // Add 'cur' to the appropriate free-list.
  58. cur->NextInPool = FreeLists[freeListIndex];
  59. FreeLists[freeListIndex] = cur;
  60. cur = next;
  61. } while (cur);
  62. }
  63. void AttributePool::takePool(AttributeList *pool) {
  64. assert(pool);
  65. // Fast path: this pool is empty.
  66. if (!Head) {
  67. Head = pool;
  68. return;
  69. }
  70. // Reverse the pool onto the current head. This optimizes for the
  71. // pattern of pulling a lot of pools into a single pool.
  72. do {
  73. AttributeList *next = pool->NextInPool;
  74. pool->NextInPool = Head;
  75. Head = pool;
  76. pool = next;
  77. } while (pool);
  78. }
  79. AttributeList *
  80. AttributePool::createIntegerAttribute(ASTContext &C, IdentifierInfo *Name,
  81. SourceLocation TokLoc, int Arg) {
  82. Expr *IArg = IntegerLiteral::Create(C, llvm::APInt(32, (uint64_t) Arg),
  83. C.IntTy, TokLoc);
  84. return create(Name, TokLoc, 0, TokLoc, 0, TokLoc, &IArg, 1,
  85. AttributeList::AS_GNU);
  86. }
  87. #include "clang/Sema/AttrParsedAttrKinds.inc"
  88. AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name,
  89. const IdentifierInfo *ScopeName,
  90. Syntax SyntaxUsed) {
  91. StringRef AttrName = Name->getName();
  92. // Normalize the attribute name, __foo__ becomes foo.
  93. if (AttrName.startswith("__") && AttrName.endswith("__") &&
  94. AttrName.size() >= 4)
  95. AttrName = AttrName.substr(2, AttrName.size() - 4);
  96. SmallString<64> Buf;
  97. if (ScopeName)
  98. Buf += ScopeName->getName();
  99. // Ensure that in the case of C++11 attributes, we look for '::foo' if it is
  100. // unscoped.
  101. if (ScopeName || SyntaxUsed == AS_CXX11)
  102. Buf += "::";
  103. Buf += AttrName;
  104. return ::getAttrKind(Buf);
  105. }