Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 1 | //===--- AttributeList.cpp --------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 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. |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the AttributeList class implementation |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 14 | #include "clang/Sema/AttributeList.h" |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame^] | 15 | #include "clang/Sema/SemaInternal.h" |
Benjamin Kramer | 1ea8e09 | 2012-07-04 17:04:04 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame^] | 18 | #include "clang/AST/DeclCXX.h" |
| 19 | #include "clang/AST/DeclTemplate.h" |
Chris Lattner | 7e0fe44 | 2009-04-11 18:48:18 +0000 | [diff] [blame] | 20 | #include "clang/Basic/IdentifierTable.h" |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringSwitch.h" |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
Richard Smith | feefaf5 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 25 | IdentifierLoc *IdentifierLoc::create(ASTContext &Ctx, SourceLocation Loc, |
| 26 | IdentifierInfo *Ident) { |
| 27 | IdentifierLoc *Result = new (Ctx) IdentifierLoc; |
| 28 | Result->Loc = Loc; |
| 29 | Result->Ident = Ident; |
| 30 | return Result; |
| 31 | } |
| 32 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 33 | size_t AttributeList::allocated_size() const { |
| 34 | if (IsAvailability) return AttributeFactory::AvailabilityAllocSize; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 35 | else if (IsTypeTagForDatatype) |
| 36 | return AttributeFactory::TypeTagForDatatypeAllocSize; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 37 | else if (IsProperty) |
| 38 | return AttributeFactory::PropertyAllocSize; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 39 | return (sizeof(AttributeList) + NumArgs * sizeof(ArgsUnion)); |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 40 | } |
Chris Lattner | f18c743 | 2008-02-20 23:14:47 +0000 | [diff] [blame] | 41 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 42 | AttributeFactory::AttributeFactory() { |
| 43 | // Go ahead and configure all the inline capacity. This is just a memset. |
| 44 | FreeLists.resize(InlineFreeListsCapacity); |
| 45 | } |
| 46 | AttributeFactory::~AttributeFactory() {} |
| 47 | |
| 48 | static size_t getFreeListIndexForSize(size_t size) { |
| 49 | assert(size >= sizeof(AttributeList)); |
| 50 | assert((size % sizeof(void*)) == 0); |
| 51 | return ((size - sizeof(AttributeList)) / sizeof(void*)); |
| 52 | } |
| 53 | |
| 54 | void *AttributeFactory::allocate(size_t size) { |
| 55 | // Check for a previously reclaimed attribute. |
| 56 | size_t index = getFreeListIndexForSize(size); |
| 57 | if (index < FreeLists.size()) { |
| 58 | if (AttributeList *attr = FreeLists[index]) { |
| 59 | FreeLists[index] = attr->NextInPool; |
| 60 | return attr; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // Otherwise, allocate something new. |
| 65 | return Alloc.Allocate(size, llvm::AlignOf<AttributeFactory>::Alignment); |
| 66 | } |
| 67 | |
| 68 | void AttributeFactory::reclaimPool(AttributeList *cur) { |
| 69 | assert(cur && "reclaiming empty pool!"); |
| 70 | do { |
| 71 | // Read this here, because we're going to overwrite NextInPool |
| 72 | // when we toss 'cur' into the appropriate queue. |
| 73 | AttributeList *next = cur->NextInPool; |
| 74 | |
| 75 | size_t size = cur->allocated_size(); |
| 76 | size_t freeListIndex = getFreeListIndexForSize(size); |
| 77 | |
| 78 | // Expand FreeLists to the appropriate size, if required. |
| 79 | if (freeListIndex >= FreeLists.size()) |
| 80 | FreeLists.resize(freeListIndex+1); |
| 81 | |
| 82 | // Add 'cur' to the appropriate free-list. |
| 83 | cur->NextInPool = FreeLists[freeListIndex]; |
| 84 | FreeLists[freeListIndex] = cur; |
| 85 | |
| 86 | cur = next; |
| 87 | } while (cur); |
| 88 | } |
| 89 | |
| 90 | void AttributePool::takePool(AttributeList *pool) { |
| 91 | assert(pool); |
| 92 | |
| 93 | // Fast path: this pool is empty. |
| 94 | if (!Head) { |
| 95 | Head = pool; |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | // Reverse the pool onto the current head. This optimizes for the |
| 100 | // pattern of pulling a lot of pools into a single pool. |
| 101 | do { |
| 102 | AttributeList *next = pool->NextInPool; |
| 103 | pool->NextInPool = Head; |
| 104 | Head = pool; |
| 105 | pool = next; |
| 106 | } while (pool); |
| 107 | } |
| 108 | |
| 109 | AttributeList * |
| 110 | AttributePool::createIntegerAttribute(ASTContext &C, IdentifierInfo *Name, |
| 111 | SourceLocation TokLoc, int Arg) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 112 | ArgsUnion IArg = IntegerLiteral::Create(C, llvm::APInt(32, (uint64_t) Arg), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 113 | C.IntTy, TokLoc); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 114 | return create(Name, TokLoc, 0, TokLoc, &IArg, 1, AttributeList::AS_GNU); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Douglas Gregor | 377f99b | 2012-05-02 17:33:51 +0000 | [diff] [blame] | 117 | #include "clang/Sema/AttrParsedAttrKinds.inc" |
| 118 | |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 119 | AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name, |
Alexis Hunt | a0e54d4 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 120 | const IdentifierInfo *ScopeName, |
| 121 | Syntax SyntaxUsed) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 122 | StringRef AttrName = Name->getName(); |
Chris Lattner | f18c743 | 2008-02-20 23:14:47 +0000 | [diff] [blame] | 123 | |
| 124 | // Normalize the attribute name, __foo__ becomes foo. |
Richard Smith | 2cca7b5 | 2012-02-25 10:41:10 +0000 | [diff] [blame] | 125 | if (AttrName.startswith("__") && AttrName.endswith("__") && |
| 126 | AttrName.size() >= 4) |
Daniel Dunbar | 5bf78579 | 2009-10-17 18:12:29 +0000 | [diff] [blame] | 127 | AttrName = AttrName.substr(2, AttrName.size() - 4); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 129 | SmallString<64> Buf; |
| 130 | if (ScopeName) |
Alexis Hunt | a0e54d4 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 131 | Buf += ScopeName->getName(); |
| 132 | // Ensure that in the case of C++11 attributes, we look for '::foo' if it is |
| 133 | // unscoped. |
| 134 | if (ScopeName || SyntaxUsed == AS_CXX11) |
| 135 | Buf += "::"; |
| 136 | Buf += AttrName; |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 137 | |
Alexis Hunt | a0e54d4 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 138 | return ::getAttrKind(Buf); |
Chris Lattner | f18c743 | 2008-02-20 23:14:47 +0000 | [diff] [blame] | 139 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 140 | |
| 141 | unsigned AttributeList::getAttributeSpellingListIndex() const { |
| 142 | // Both variables will be used in tablegen generated |
| 143 | // attribute spell list index matching code. |
| 144 | StringRef Name = AttrName->getName(); |
| 145 | StringRef Scope = ScopeName ? ScopeName->getName() : ""; |
| 146 | |
| 147 | #include "clang/Sema/AttrSpellingListIndex.inc" |
| 148 | |
| 149 | } |
| 150 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 151 | struct ParsedAttrInfo { |
| 152 | unsigned NumArgs : 4; |
| 153 | unsigned OptArgs : 4; |
| 154 | unsigned HasCustomParsing : 1; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame^] | 155 | |
| 156 | bool (*DiagAppertainsToDecl)(Sema &S, const AttributeList &Attr, |
| 157 | const Decl *); |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | namespace { |
| 161 | #include "clang/Sema/AttrParsedAttrImpl.inc" |
| 162 | } |
| 163 | |
Benjamin Kramer | 602623f | 2013-09-28 15:08:41 +0000 | [diff] [blame] | 164 | static const ParsedAttrInfo &getInfo(const AttributeList &A) { |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 165 | return AttrInfoMap[A.getKind()]; |
| 166 | } |
| 167 | |
| 168 | unsigned AttributeList::getMinArgs() const { |
| 169 | return getInfo(*this).NumArgs; |
| 170 | } |
| 171 | |
| 172 | unsigned AttributeList::getMaxArgs() const { |
| 173 | return getMinArgs() + getInfo(*this).OptArgs; |
| 174 | } |
| 175 | |
| 176 | bool AttributeList::hasCustomParsing() const { |
| 177 | return getInfo(*this).HasCustomParsing; |
| 178 | } |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame^] | 179 | |
| 180 | bool AttributeList::diagnoseAppertainsTo(Sema &S, const Decl *D) const { |
| 181 | return getInfo(*this).DiagAppertainsToDecl(S, *this, D); |
| 182 | } |