blob: 0f209fd7d6239b7c9d235ac9d881b36d289427a2 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- AttributeList.cpp --------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the AttributeList class implementation
11//
12//===----------------------------------------------------------------------===//
13
John McCall19510852010-08-20 18:27:03 +000014#include "clang/Sema/AttributeList.h"
Benjamin Kramer478851c2012-07-04 17:04:04 +000015#include "clang/AST/ASTContext.h"
John McCall0b7e6782011-03-24 11:26:52 +000016#include "clang/AST/Expr.h"
Chris Lattner8f823d22009-04-11 18:48:18 +000017#include "clang/Basic/IdentifierTable.h"
Douglas Gregord6268ff2012-05-02 14:50:50 +000018#include "llvm/ADT/StringSwitch.h"
Richard Smithe0d3b4c2012-05-03 18:27:39 +000019#include "llvm/ADT/SmallString.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
John McCall0b7e6782011-03-24 11:26:52 +000022size_t AttributeList::allocated_size() const {
23 if (IsAvailability) return AttributeFactory::AvailabilityAllocSize;
24 return (sizeof(AttributeList) + NumArgs * sizeof(Expr*));
Reid Spencer5f016e22007-07-11 17:01:13 +000025}
Chris Lattner23351912008-02-20 23:14:47 +000026
John McCall0b7e6782011-03-24 11:26:52 +000027AttributeFactory::AttributeFactory() {
28 // Go ahead and configure all the inline capacity. This is just a memset.
29 FreeLists.resize(InlineFreeListsCapacity);
30}
31AttributeFactory::~AttributeFactory() {}
32
33static size_t getFreeListIndexForSize(size_t size) {
34 assert(size >= sizeof(AttributeList));
35 assert((size % sizeof(void*)) == 0);
36 return ((size - sizeof(AttributeList)) / sizeof(void*));
37}
38
39void *AttributeFactory::allocate(size_t size) {
40 // Check for a previously reclaimed attribute.
41 size_t index = getFreeListIndexForSize(size);
42 if (index < FreeLists.size()) {
43 if (AttributeList *attr = FreeLists[index]) {
44 FreeLists[index] = attr->NextInPool;
45 return attr;
46 }
47 }
48
49 // Otherwise, allocate something new.
50 return Alloc.Allocate(size, llvm::AlignOf<AttributeFactory>::Alignment);
51}
52
53void AttributeFactory::reclaimPool(AttributeList *cur) {
54 assert(cur && "reclaiming empty pool!");
55 do {
56 // Read this here, because we're going to overwrite NextInPool
57 // when we toss 'cur' into the appropriate queue.
58 AttributeList *next = cur->NextInPool;
59
60 size_t size = cur->allocated_size();
61 size_t freeListIndex = getFreeListIndexForSize(size);
62
63 // Expand FreeLists to the appropriate size, if required.
64 if (freeListIndex >= FreeLists.size())
65 FreeLists.resize(freeListIndex+1);
66
67 // Add 'cur' to the appropriate free-list.
68 cur->NextInPool = FreeLists[freeListIndex];
69 FreeLists[freeListIndex] = cur;
70
71 cur = next;
72 } while (cur);
73}
74
75void AttributePool::takePool(AttributeList *pool) {
76 assert(pool);
77
78 // Fast path: this pool is empty.
79 if (!Head) {
80 Head = pool;
81 return;
82 }
83
84 // Reverse the pool onto the current head. This optimizes for the
85 // pattern of pulling a lot of pools into a single pool.
86 do {
87 AttributeList *next = pool->NextInPool;
88 pool->NextInPool = Head;
89 Head = pool;
90 pool = next;
91 } while (pool);
92}
93
94AttributeList *
95AttributePool::createIntegerAttribute(ASTContext &C, IdentifierInfo *Name,
96 SourceLocation TokLoc, int Arg) {
97 Expr *IArg = IntegerLiteral::Create(C, llvm::APInt(32, (uint64_t) Arg),
98 C.IntTy, TokLoc);
Sean Hunt93f95f22012-06-18 16:13:52 +000099 return create(Name, TokLoc, 0, TokLoc, 0, TokLoc, &IArg, 1,
100 AttributeList::AS_GNU);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000101}
102
Douglas Gregor0c19b3c2012-05-02 17:33:51 +0000103#include "clang/Sema/AttrParsedAttrKinds.inc"
104
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000105AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name,
Sean Hunt93f95f22012-06-18 16:13:52 +0000106 const IdentifierInfo *ScopeName,
107 Syntax SyntaxUsed) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000108 StringRef AttrName = Name->getName();
Chris Lattner23351912008-02-20 23:14:47 +0000109
110 // Normalize the attribute name, __foo__ becomes foo.
Richard Smith5297d712012-02-25 10:41:10 +0000111 if (AttrName.startswith("__") && AttrName.endswith("__") &&
112 AttrName.size() >= 4)
Daniel Dunbar4f90d8d2009-10-17 18:12:29 +0000113 AttrName = AttrName.substr(2, AttrName.size() - 4);
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000115 SmallString<64> Buf;
116 if (ScopeName)
Sean Hunt93f95f22012-06-18 16:13:52 +0000117 Buf += ScopeName->getName();
118 // Ensure that in the case of C++11 attributes, we look for '::foo' if it is
119 // unscoped.
120 if (ScopeName || SyntaxUsed == AS_CXX11)
121 Buf += "::";
122 Buf += AttrName;
Richard Smithe0d3b4c2012-05-03 18:27:39 +0000123
Sean Hunt93f95f22012-06-18 16:13:52 +0000124 return ::getAttrKind(Buf);
Chris Lattner23351912008-02-20 23:14:47 +0000125}