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