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