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 IdentifierLoc *IdentifierLoc::create(ASTContext &Ctx, SourceLocation Loc,
23 IdentifierInfo *Ident) {
24 IdentifierLoc *Result = new (Ctx) IdentifierLoc;
26 Result->Ident = Ident;
30 size_t AttributeList::allocated_size() const {
31 if (IsAvailability) return AttributeFactory::AvailabilityAllocSize;
32 else if (IsTypeTagForDatatype)
33 return AttributeFactory::TypeTagForDatatypeAllocSize;
35 return AttributeFactory::PropertyAllocSize;
36 return (sizeof(AttributeList) + NumArgs * sizeof(ArgsUnion));
39 AttributeFactory::AttributeFactory() {
40 // Go ahead and configure all the inline capacity. This is just a memset.
41 FreeLists.resize(InlineFreeListsCapacity);
43 AttributeFactory::~AttributeFactory() {}
45 static size_t getFreeListIndexForSize(size_t size) {
46 assert(size >= sizeof(AttributeList));
47 assert((size % sizeof(void*)) == 0);
48 return ((size - sizeof(AttributeList)) / sizeof(void*));
51 void *AttributeFactory::allocate(size_t size) {
52 // Check for a previously reclaimed attribute.
53 size_t index = getFreeListIndexForSize(size);
54 if (index < FreeLists.size()) {
55 if (AttributeList *attr = FreeLists[index]) {
56 FreeLists[index] = attr->NextInPool;
61 // Otherwise, allocate something new.
62 return Alloc.Allocate(size, llvm::AlignOf<AttributeFactory>::Alignment);
65 void AttributeFactory::reclaimPool(AttributeList *cur) {
66 assert(cur && "reclaiming empty pool!");
68 // Read this here, because we're going to overwrite NextInPool
69 // when we toss 'cur' into the appropriate queue.
70 AttributeList *next = cur->NextInPool;
72 size_t size = cur->allocated_size();
73 size_t freeListIndex = getFreeListIndexForSize(size);
75 // Expand FreeLists to the appropriate size, if required.
76 if (freeListIndex >= FreeLists.size())
77 FreeLists.resize(freeListIndex+1);
79 // Add 'cur' to the appropriate free-list.
80 cur->NextInPool = FreeLists[freeListIndex];
81 FreeLists[freeListIndex] = cur;
87 void AttributePool::takePool(AttributeList *pool) {
90 // Fast path: this pool is empty.
96 // Reverse the pool onto the current head. This optimizes for the
97 // pattern of pulling a lot of pools into a single pool.
99 AttributeList *next = pool->NextInPool;
100 pool->NextInPool = Head;
107 AttributePool::createIntegerAttribute(ASTContext &C, IdentifierInfo *Name,
108 SourceLocation TokLoc, int Arg) {
109 ArgsUnion IArg = IntegerLiteral::Create(C, llvm::APInt(32, (uint64_t) Arg),
111 return create(Name, TokLoc, 0, TokLoc, &IArg, 1, AttributeList::AS_GNU);
114 #include "clang/Sema/AttrParsedAttrKinds.inc"
116 AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name,
117 const IdentifierInfo *ScopeName,
119 StringRef AttrName = Name->getName();
121 // Normalize the attribute name, __foo__ becomes foo.
122 if (AttrName.startswith("__") && AttrName.endswith("__") &&
123 AttrName.size() >= 4)
124 AttrName = AttrName.substr(2, AttrName.size() - 4);
128 Buf += ScopeName->getName();
129 // Ensure that in the case of C++11 attributes, we look for '::foo' if it is
131 if (ScopeName || SyntaxUsed == AS_CXX11)
135 return ::getAttrKind(Buf);
138 unsigned AttributeList::getAttributeSpellingListIndex() const {
139 // Both variables will be used in tablegen generated
140 // attribute spell list index matching code.
141 StringRef Name = AttrName->getName();
142 StringRef Scope = ScopeName ? ScopeName->getName() : "";
144 #include "clang/Sema/AttrSpellingListIndex.inc"
148 struct ParsedAttrInfo {
149 unsigned NumArgs : 4;
150 unsigned OptArgs : 4;
151 unsigned HasCustomParsing : 1;
155 #include "clang/Sema/AttrParsedAttrImpl.inc"
158 static const ParsedAttrInfo &getInfo(const AttributeList &A) {
159 return AttrInfoMap[A.getKind()];
162 unsigned AttributeList::getMinArgs() const {
163 return getInfo(*this).NumArgs;
166 unsigned AttributeList::getMaxArgs() const {
167 return getMinArgs() + getInfo(*this).OptArgs;
170 bool AttributeList::hasCustomParsing() const {
171 return getInfo(*this).HasCustomParsing;