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