Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- AttributeList.cpp --------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the AttributeList class implementation |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 14 | #include "clang/Sema/AttributeList.h" |
Benjamin Kramer | 478851c | 2012-07-04 17:04:04 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 16 | #include "clang/AST/Expr.h" |
Chris Lattner | 8f823d2 | 2009-04-11 18:48:18 +0000 | [diff] [blame] | 17 | #include "clang/Basic/IdentifierTable.h" |
Douglas Gregor | d6268ff | 2012-05-02 14:50:50 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringSwitch.h" |
Richard Smith | e0d3b4c | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 22 | size_t AttributeList::allocated_size() const { |
| 23 | if (IsAvailability) return AttributeFactory::AvailabilityAllocSize; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 24 | else if (IsTypeTagForDatatype) |
| 25 | return AttributeFactory::TypeTagForDatatypeAllocSize; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 26 | return (sizeof(AttributeList) + NumArgs * sizeof(Expr*)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | } |
Chris Lattner | 2335191 | 2008-02-20 23:14:47 +0000 | [diff] [blame] | 28 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 29 | AttributeFactory::AttributeFactory() { |
| 30 | // Go ahead and configure all the inline capacity. This is just a memset. |
| 31 | FreeLists.resize(InlineFreeListsCapacity); |
| 32 | } |
| 33 | AttributeFactory::~AttributeFactory() {} |
| 34 | |
| 35 | static size_t getFreeListIndexForSize(size_t size) { |
| 36 | assert(size >= sizeof(AttributeList)); |
| 37 | assert((size % sizeof(void*)) == 0); |
| 38 | return ((size - sizeof(AttributeList)) / sizeof(void*)); |
| 39 | } |
| 40 | |
| 41 | void *AttributeFactory::allocate(size_t size) { |
| 42 | // Check for a previously reclaimed attribute. |
| 43 | size_t index = getFreeListIndexForSize(size); |
| 44 | if (index < FreeLists.size()) { |
| 45 | if (AttributeList *attr = FreeLists[index]) { |
| 46 | FreeLists[index] = attr->NextInPool; |
| 47 | return attr; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // Otherwise, allocate something new. |
| 52 | return Alloc.Allocate(size, llvm::AlignOf<AttributeFactory>::Alignment); |
| 53 | } |
| 54 | |
| 55 | void AttributeFactory::reclaimPool(AttributeList *cur) { |
| 56 | assert(cur && "reclaiming empty pool!"); |
| 57 | do { |
| 58 | // Read this here, because we're going to overwrite NextInPool |
| 59 | // when we toss 'cur' into the appropriate queue. |
| 60 | AttributeList *next = cur->NextInPool; |
| 61 | |
| 62 | size_t size = cur->allocated_size(); |
| 63 | size_t freeListIndex = getFreeListIndexForSize(size); |
| 64 | |
| 65 | // Expand FreeLists to the appropriate size, if required. |
| 66 | if (freeListIndex >= FreeLists.size()) |
| 67 | FreeLists.resize(freeListIndex+1); |
| 68 | |
| 69 | // Add 'cur' to the appropriate free-list. |
| 70 | cur->NextInPool = FreeLists[freeListIndex]; |
| 71 | FreeLists[freeListIndex] = cur; |
| 72 | |
| 73 | cur = next; |
| 74 | } while (cur); |
| 75 | } |
| 76 | |
| 77 | void AttributePool::takePool(AttributeList *pool) { |
| 78 | assert(pool); |
| 79 | |
| 80 | // Fast path: this pool is empty. |
| 81 | if (!Head) { |
| 82 | Head = pool; |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | // Reverse the pool onto the current head. This optimizes for the |
| 87 | // pattern of pulling a lot of pools into a single pool. |
| 88 | do { |
| 89 | AttributeList *next = pool->NextInPool; |
| 90 | pool->NextInPool = Head; |
| 91 | Head = pool; |
| 92 | pool = next; |
| 93 | } while (pool); |
| 94 | } |
| 95 | |
| 96 | AttributeList * |
| 97 | AttributePool::createIntegerAttribute(ASTContext &C, IdentifierInfo *Name, |
| 98 | SourceLocation TokLoc, int Arg) { |
| 99 | Expr *IArg = IntegerLiteral::Create(C, llvm::APInt(32, (uint64_t) Arg), |
| 100 | C.IntTy, TokLoc); |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 101 | return create(Name, TokLoc, 0, TokLoc, 0, TokLoc, &IArg, 1, |
| 102 | AttributeList::AS_GNU); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Douglas Gregor | 0c19b3c | 2012-05-02 17:33:51 +0000 | [diff] [blame] | 105 | #include "clang/Sema/AttrParsedAttrKinds.inc" |
| 106 | |
Richard Smith | e0d3b4c | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 107 | AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name, |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 108 | const IdentifierInfo *ScopeName, |
| 109 | Syntax SyntaxUsed) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 110 | StringRef AttrName = Name->getName(); |
Chris Lattner | 2335191 | 2008-02-20 23:14:47 +0000 | [diff] [blame] | 111 | |
| 112 | // Normalize the attribute name, __foo__ becomes foo. |
Richard Smith | 5297d71 | 2012-02-25 10:41:10 +0000 | [diff] [blame] | 113 | if (AttrName.startswith("__") && AttrName.endswith("__") && |
| 114 | AttrName.size() >= 4) |
Daniel Dunbar | 4f90d8d | 2009-10-17 18:12:29 +0000 | [diff] [blame] | 115 | AttrName = AttrName.substr(2, AttrName.size() - 4); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | |
Richard Smith | e0d3b4c | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 117 | SmallString<64> Buf; |
| 118 | if (ScopeName) |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 119 | Buf += ScopeName->getName(); |
| 120 | // Ensure that in the case of C++11 attributes, we look for '::foo' if it is |
| 121 | // unscoped. |
| 122 | if (ScopeName || SyntaxUsed == AS_CXX11) |
| 123 | Buf += "::"; |
| 124 | Buf += AttrName; |
Richard Smith | e0d3b4c | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 125 | |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 126 | return ::getAttrKind(Buf); |
Chris Lattner | 2335191 | 2008-02-20 23:14:47 +0000 | [diff] [blame] | 127 | } |