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" |
Richard Smith | e0d3b4c | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringSwitch.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Richard Smith | 8edabd9 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 22 | IdentifierLoc *IdentifierLoc::create(ASTContext &Ctx, SourceLocation Loc, |
| 23 | IdentifierInfo *Ident) { |
| 24 | IdentifierLoc *Result = new (Ctx) IdentifierLoc; |
| 25 | Result->Loc = Loc; |
| 26 | Result->Ident = Ident; |
| 27 | return Result; |
| 28 | } |
| 29 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 30 | size_t AttributeList::allocated_size() const { |
| 31 | if (IsAvailability) return AttributeFactory::AvailabilityAllocSize; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 32 | else if (IsTypeTagForDatatype) |
| 33 | return AttributeFactory::TypeTagForDatatypeAllocSize; |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 34 | else if (IsProperty) |
| 35 | return AttributeFactory::PropertyAllocSize; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 36 | return (sizeof(AttributeList) + NumArgs * sizeof(ArgsUnion)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 37 | } |
Chris Lattner | 2335191 | 2008-02-20 23:14:47 +0000 | [diff] [blame] | 38 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 39 | AttributeFactory::AttributeFactory() { |
| 40 | // Go ahead and configure all the inline capacity. This is just a memset. |
| 41 | FreeLists.resize(InlineFreeListsCapacity); |
| 42 | } |
| 43 | AttributeFactory::~AttributeFactory() {} |
| 44 | |
| 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*)); |
| 49 | } |
| 50 | |
| 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; |
| 57 | return attr; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Otherwise, allocate something new. |
| 62 | return Alloc.Allocate(size, llvm::AlignOf<AttributeFactory>::Alignment); |
| 63 | } |
| 64 | |
| 65 | void AttributeFactory::reclaimPool(AttributeList *cur) { |
| 66 | assert(cur && "reclaiming empty pool!"); |
| 67 | do { |
| 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; |
| 71 | |
| 72 | size_t size = cur->allocated_size(); |
| 73 | size_t freeListIndex = getFreeListIndexForSize(size); |
| 74 | |
| 75 | // Expand FreeLists to the appropriate size, if required. |
| 76 | if (freeListIndex >= FreeLists.size()) |
| 77 | FreeLists.resize(freeListIndex+1); |
| 78 | |
| 79 | // Add 'cur' to the appropriate free-list. |
| 80 | cur->NextInPool = FreeLists[freeListIndex]; |
| 81 | FreeLists[freeListIndex] = cur; |
| 82 | |
| 83 | cur = next; |
| 84 | } while (cur); |
| 85 | } |
| 86 | |
| 87 | void AttributePool::takePool(AttributeList *pool) { |
| 88 | assert(pool); |
| 89 | |
| 90 | // Fast path: this pool is empty. |
| 91 | if (!Head) { |
| 92 | Head = pool; |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | // Reverse the pool onto the current head. This optimizes for the |
| 97 | // pattern of pulling a lot of pools into a single pool. |
| 98 | do { |
| 99 | AttributeList *next = pool->NextInPool; |
| 100 | pool->NextInPool = Head; |
| 101 | Head = pool; |
| 102 | pool = next; |
| 103 | } while (pool); |
| 104 | } |
| 105 | |
| 106 | AttributeList * |
| 107 | AttributePool::createIntegerAttribute(ASTContext &C, IdentifierInfo *Name, |
| 108 | SourceLocation TokLoc, int Arg) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 109 | ArgsUnion IArg = IntegerLiteral::Create(C, llvm::APInt(32, (uint64_t) Arg), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 110 | C.IntTy, TokLoc); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 111 | return create(Name, TokLoc, 0, TokLoc, &IArg, 1, AttributeList::AS_GNU); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Douglas Gregor | 0c19b3c | 2012-05-02 17:33:51 +0000 | [diff] [blame] | 114 | #include "clang/Sema/AttrParsedAttrKinds.inc" |
| 115 | |
Richard Smith | e0d3b4c | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 116 | AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name, |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 117 | const IdentifierInfo *ScopeName, |
| 118 | Syntax SyntaxUsed) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 119 | StringRef AttrName = Name->getName(); |
Chris Lattner | 2335191 | 2008-02-20 23:14:47 +0000 | [diff] [blame] | 120 | |
| 121 | // Normalize the attribute name, __foo__ becomes foo. |
Richard Smith | 5297d71 | 2012-02-25 10:41:10 +0000 | [diff] [blame] | 122 | if (AttrName.startswith("__") && AttrName.endswith("__") && |
| 123 | AttrName.size() >= 4) |
Daniel Dunbar | 4f90d8d | 2009-10-17 18:12:29 +0000 | [diff] [blame] | 124 | AttrName = AttrName.substr(2, AttrName.size() - 4); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | |
Richard Smith | e0d3b4c | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 126 | SmallString<64> Buf; |
| 127 | if (ScopeName) |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 128 | Buf += ScopeName->getName(); |
| 129 | // Ensure that in the case of C++11 attributes, we look for '::foo' if it is |
| 130 | // unscoped. |
| 131 | if (ScopeName || SyntaxUsed == AS_CXX11) |
| 132 | Buf += "::"; |
| 133 | Buf += AttrName; |
Richard Smith | e0d3b4c | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 134 | |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 135 | return ::getAttrKind(Buf); |
Chris Lattner | 2335191 | 2008-02-20 23:14:47 +0000 | [diff] [blame] | 136 | } |
Michael Han | 51d8c52 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 137 | |
| 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() : ""; |
| 143 | |
| 144 | #include "clang/Sema/AttrSpellingListIndex.inc" |
| 145 | |
| 146 | } |
| 147 | |
Aaron Ballman | bbb3b32 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 148 | struct ParsedAttrInfo { |
| 149 | unsigned NumArgs : 4; |
| 150 | unsigned OptArgs : 4; |
| 151 | unsigned HasCustomParsing : 1; |
| 152 | }; |
| 153 | |
| 154 | namespace { |
| 155 | #include "clang/Sema/AttrParsedAttrImpl.inc" |
| 156 | } |
| 157 | |
Benjamin Kramer | 6ad3cdd | 2013-09-28 15:08:41 +0000 | [diff] [blame] | 158 | static const ParsedAttrInfo &getInfo(const AttributeList &A) { |
Aaron Ballman | bbb3b32 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 159 | return AttrInfoMap[A.getKind()]; |
| 160 | } |
| 161 | |
| 162 | unsigned AttributeList::getMinArgs() const { |
| 163 | return getInfo(*this).NumArgs; |
| 164 | } |
| 165 | |
| 166 | unsigned AttributeList::getMaxArgs() const { |
| 167 | return getMinArgs() + getInfo(*this).OptArgs; |
| 168 | } |
| 169 | |
| 170 | bool AttributeList::hasCustomParsing() const { |
| 171 | return getInfo(*this).HasCustomParsing; |
| 172 | } |