1 //===--- AttributeList.cpp --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the AttributeList class implementation
12 //===----------------------------------------------------------------------===//
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;
22 size_t AttributeList::allocated_size() const {
23 if (IsAvailability) return AttributeFactory::AvailabilityAllocSize;
24 else if (IsTypeTagForDatatype)
25 return AttributeFactory::TypeTagForDatatypeAllocSize;
27 return AttributeFactory::PropertyAllocSize;
28 return (sizeof(AttributeList) + NumArgs * sizeof(Expr*));
31 AttributeFactory::AttributeFactory() {
32 // Go ahead and configure all the inline capacity. This is just a memset.
33 FreeLists.resize(InlineFreeListsCapacity);
35 AttributeFactory::~AttributeFactory() {}
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*));
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;
53 // Otherwise, allocate something new.
54 return Alloc.Allocate(size, llvm::AlignOf<AttributeFactory>::Alignment);
57 void AttributeFactory::reclaimPool(AttributeList *cur) {
58 assert(cur && "reclaiming empty pool!");
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;
64 size_t size = cur->allocated_size();
65 size_t freeListIndex = getFreeListIndexForSize(size);
67 // Expand FreeLists to the appropriate size, if required.
68 if (freeListIndex >= FreeLists.size())
69 FreeLists.resize(freeListIndex+1);
71 // Add 'cur' to the appropriate free-list.
72 cur->NextInPool = FreeLists[freeListIndex];
73 FreeLists[freeListIndex] = cur;
79 void AttributePool::takePool(AttributeList *pool) {
82 // Fast path: this pool is empty.
88 // Reverse the pool onto the current head. This optimizes for the
89 // pattern of pulling a lot of pools into a single pool.
91 AttributeList *next = pool->NextInPool;
92 pool->NextInPool = Head;
99 AttributePool::createIntegerAttribute(ASTContext &C, IdentifierInfo *Name,
100 SourceLocation TokLoc, int Arg) {
101 Expr *IArg = IntegerLiteral::Create(C, llvm::APInt(32, (uint64_t) Arg),
103 return create(Name, TokLoc, 0, TokLoc, 0, TokLoc, &IArg, 1,
104 AttributeList::AS_GNU);
107 #include "clang/Sema/AttrParsedAttrKinds.inc"
109 AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name,
110 const IdentifierInfo *ScopeName,
112 StringRef AttrName = Name->getName();
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);
121 Buf += ScopeName->getName();
122 // Ensure that in the case of C++11 attributes, we look for '::foo' if it is
124 if (ScopeName || SyntaxUsed == AS_CXX11)
128 return ::getAttrKind(Buf);
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() : "";
137 #include "clang/Sema/AttrSpellingListIndex.inc"