Eugene Zelenko | 711964d | 2018-02-20 02:16:28 +0000 | [diff] [blame^] | 1 | //===- AttributeList.cpp --------------------------------------------------===// |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 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" |
Benjamin Kramer | 1ea8e09 | 2012-07-04 17:04:04 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 16 | #include "clang/Basic/AttrSubjectMatchRules.h" |
Chris Lattner | 7e0fe44 | 2009-04-11 18:48:18 +0000 | [diff] [blame] | 17 | #include "clang/Basic/IdentifierTable.h" |
Bob Wilson | 7c73083 | 2015-07-20 22:57:31 +0000 | [diff] [blame] | 18 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 19 | #include "clang/Sema/SemaInternal.h" |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | 711964d | 2018-02-20 02:16:28 +0000 | [diff] [blame^] | 21 | #include "llvm/ADT/SmallVector.h" |
| 22 | #include "llvm/ADT/StringRef.h" |
| 23 | #include <cassert> |
| 24 | #include <cstddef> |
| 25 | #include <utility> |
| 26 | |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | |
Richard Smith | feefaf5 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 29 | IdentifierLoc *IdentifierLoc::create(ASTContext &Ctx, SourceLocation Loc, |
| 30 | IdentifierInfo *Ident) { |
| 31 | IdentifierLoc *Result = new (Ctx) IdentifierLoc; |
| 32 | Result->Loc = Loc; |
| 33 | Result->Ident = Ident; |
| 34 | return Result; |
| 35 | } |
| 36 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 37 | size_t AttributeList::allocated_size() const { |
| 38 | if (IsAvailability) return AttributeFactory::AvailabilityAllocSize; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 39 | else if (IsTypeTagForDatatype) |
| 40 | return AttributeFactory::TypeTagForDatatypeAllocSize; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 41 | else if (IsProperty) |
| 42 | return AttributeFactory::PropertyAllocSize; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 43 | return (sizeof(AttributeList) + NumArgs * sizeof(ArgsUnion)); |
Steve Naroff | b8371e1 | 2007-06-09 03:39:29 +0000 | [diff] [blame] | 44 | } |
Chris Lattner | f18c743 | 2008-02-20 23:14:47 +0000 | [diff] [blame] | 45 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 46 | AttributeFactory::AttributeFactory() { |
| 47 | // Go ahead and configure all the inline capacity. This is just a memset. |
| 48 | FreeLists.resize(InlineFreeListsCapacity); |
| 49 | } |
Eugene Zelenko | 711964d | 2018-02-20 02:16:28 +0000 | [diff] [blame^] | 50 | AttributeFactory::~AttributeFactory() = default; |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 51 | |
| 52 | static size_t getFreeListIndexForSize(size_t size) { |
| 53 | assert(size >= sizeof(AttributeList)); |
| 54 | assert((size % sizeof(void*)) == 0); |
| 55 | return ((size - sizeof(AttributeList)) / sizeof(void*)); |
| 56 | } |
| 57 | |
| 58 | void *AttributeFactory::allocate(size_t size) { |
| 59 | // Check for a previously reclaimed attribute. |
| 60 | size_t index = getFreeListIndexForSize(size); |
| 61 | if (index < FreeLists.size()) { |
| 62 | if (AttributeList *attr = FreeLists[index]) { |
| 63 | FreeLists[index] = attr->NextInPool; |
| 64 | return attr; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // Otherwise, allocate something new. |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 69 | return Alloc.Allocate(size, alignof(AttributeFactory)); |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void AttributeFactory::reclaimPool(AttributeList *cur) { |
| 73 | assert(cur && "reclaiming empty pool!"); |
| 74 | do { |
| 75 | // Read this here, because we're going to overwrite NextInPool |
| 76 | // when we toss 'cur' into the appropriate queue. |
| 77 | AttributeList *next = cur->NextInPool; |
| 78 | |
| 79 | size_t size = cur->allocated_size(); |
| 80 | size_t freeListIndex = getFreeListIndexForSize(size); |
| 81 | |
| 82 | // Expand FreeLists to the appropriate size, if required. |
| 83 | if (freeListIndex >= FreeLists.size()) |
| 84 | FreeLists.resize(freeListIndex+1); |
| 85 | |
| 86 | // Add 'cur' to the appropriate free-list. |
| 87 | cur->NextInPool = FreeLists[freeListIndex]; |
| 88 | FreeLists[freeListIndex] = cur; |
| 89 | |
| 90 | cur = next; |
| 91 | } while (cur); |
| 92 | } |
| 93 | |
| 94 | void AttributePool::takePool(AttributeList *pool) { |
| 95 | assert(pool); |
| 96 | |
| 97 | // Fast path: this pool is empty. |
| 98 | if (!Head) { |
| 99 | Head = pool; |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | // Reverse the pool onto the current head. This optimizes for the |
| 104 | // pattern of pulling a lot of pools into a single pool. |
| 105 | do { |
| 106 | AttributeList *next = pool->NextInPool; |
| 107 | pool->NextInPool = Head; |
| 108 | Head = pool; |
| 109 | pool = next; |
| 110 | } while (pool); |
| 111 | } |
| 112 | |
Douglas Gregor | 377f99b | 2012-05-02 17:33:51 +0000 | [diff] [blame] | 113 | #include "clang/Sema/AttrParsedAttrKinds.inc" |
| 114 | |
David Majnemer | a94e7b6 | 2015-08-25 16:44:38 +0000 | [diff] [blame] | 115 | static StringRef normalizeAttrName(StringRef AttrName, StringRef ScopeName, |
| 116 | AttributeList::Syntax SyntaxUsed) { |
| 117 | // Normalize the attribute name, __foo__ becomes foo. This is only allowable |
| 118 | // for GNU attributes. |
| 119 | bool IsGNU = SyntaxUsed == AttributeList::AS_GNU || |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 120 | ((SyntaxUsed == AttributeList::AS_CXX11 || |
| 121 | SyntaxUsed == AttributeList::AS_C2x) && ScopeName == "gnu"); |
David Majnemer | a94e7b6 | 2015-08-25 16:44:38 +0000 | [diff] [blame] | 122 | if (IsGNU && AttrName.size() >= 4 && AttrName.startswith("__") && |
| 123 | AttrName.endswith("__")) |
| 124 | AttrName = AttrName.slice(2, AttrName.size() - 2); |
| 125 | |
| 126 | return AttrName; |
| 127 | } |
| 128 | |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 129 | AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name, |
Alexis Hunt | a0e54d4 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 130 | const IdentifierInfo *ScopeName, |
| 131 | Syntax SyntaxUsed) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 132 | StringRef AttrName = Name->getName(); |
Chris Lattner | f18c743 | 2008-02-20 23:14:47 +0000 | [diff] [blame] | 133 | |
Aaron Ballman | c698809 | 2013-12-11 22:27:44 +0000 | [diff] [blame] | 134 | SmallString<64> FullName; |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 135 | if (ScopeName) |
Aaron Ballman | c698809 | 2013-12-11 22:27:44 +0000 | [diff] [blame] | 136 | FullName += ScopeName->getName(); |
| 137 | |
David Majnemer | a94e7b6 | 2015-08-25 16:44:38 +0000 | [diff] [blame] | 138 | AttrName = normalizeAttrName(AttrName, FullName, SyntaxUsed); |
Aaron Ballman | c698809 | 2013-12-11 22:27:44 +0000 | [diff] [blame] | 139 | |
Alexis Hunt | a0e54d4 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 140 | // Ensure that in the case of C++11 attributes, we look for '::foo' if it is |
| 141 | // unscoped. |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 142 | if (ScopeName || SyntaxUsed == AS_CXX11 || SyntaxUsed == AS_C2x) |
Aaron Ballman | c698809 | 2013-12-11 22:27:44 +0000 | [diff] [blame] | 143 | FullName += "::"; |
| 144 | FullName += AttrName; |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 145 | |
Aaron Ballman | 09e98ff | 2014-01-13 21:42:39 +0000 | [diff] [blame] | 146 | return ::getAttrKind(FullName, SyntaxUsed); |
Chris Lattner | f18c743 | 2008-02-20 23:14:47 +0000 | [diff] [blame] | 147 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 148 | |
| 149 | unsigned AttributeList::getAttributeSpellingListIndex() const { |
| 150 | // Both variables will be used in tablegen generated |
| 151 | // attribute spell list index matching code. |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 152 | StringRef Scope = ScopeName ? ScopeName->getName() : ""; |
David Majnemer | a94e7b6 | 2015-08-25 16:44:38 +0000 | [diff] [blame] | 153 | StringRef Name = normalizeAttrName(AttrName->getName(), Scope, |
| 154 | (AttributeList::Syntax)SyntaxUsed); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 155 | |
| 156 | #include "clang/Sema/AttrSpellingListIndex.inc" |
| 157 | |
| 158 | } |
| 159 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 160 | struct ParsedAttrInfo { |
| 161 | unsigned NumArgs : 4; |
| 162 | unsigned OptArgs : 4; |
| 163 | unsigned HasCustomParsing : 1; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 164 | unsigned IsTargetSpecific : 1; |
| 165 | unsigned IsType : 1; |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 166 | unsigned IsStmt : 1; |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 167 | unsigned IsKnownToGCC : 1; |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 168 | unsigned IsSupportedByPragmaAttribute : 1; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 169 | |
| 170 | bool (*DiagAppertainsToDecl)(Sema &S, const AttributeList &Attr, |
| 171 | const Decl *); |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 172 | bool (*DiagLangOpts)(Sema &S, const AttributeList &Attr); |
Bob Wilson | 7c73083 | 2015-07-20 22:57:31 +0000 | [diff] [blame] | 173 | bool (*ExistsInTarget)(const TargetInfo &Target); |
Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 174 | unsigned (*SpellingIndexToSemanticSpelling)(const AttributeList &Attr); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 175 | void (*GetPragmaAttributeMatchRules)( |
| 176 | llvm::SmallVectorImpl<std::pair<attr::SubjectMatchRule, bool>> &Rules, |
| 177 | const LangOptions &LangOpts); |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | namespace { |
Eugene Zelenko | 711964d | 2018-02-20 02:16:28 +0000 | [diff] [blame^] | 181 | |
| 182 | #include "clang/Sema/AttrParsedAttrImpl.inc" |
| 183 | |
| 184 | } // namespace |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 185 | |
Benjamin Kramer | 602623f | 2013-09-28 15:08:41 +0000 | [diff] [blame] | 186 | static const ParsedAttrInfo &getInfo(const AttributeList &A) { |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 187 | return AttrInfoMap[A.getKind()]; |
| 188 | } |
| 189 | |
| 190 | unsigned AttributeList::getMinArgs() const { |
| 191 | return getInfo(*this).NumArgs; |
| 192 | } |
| 193 | |
| 194 | unsigned AttributeList::getMaxArgs() const { |
| 195 | return getMinArgs() + getInfo(*this).OptArgs; |
| 196 | } |
| 197 | |
| 198 | bool AttributeList::hasCustomParsing() const { |
| 199 | return getInfo(*this).HasCustomParsing; |
| 200 | } |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 201 | |
| 202 | bool AttributeList::diagnoseAppertainsTo(Sema &S, const Decl *D) const { |
| 203 | return getInfo(*this).DiagAppertainsToDecl(S, *this, D); |
| 204 | } |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 205 | |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 206 | bool AttributeList::appliesToDecl(const Decl *D, |
| 207 | attr::SubjectMatchRule MatchRule) const { |
| 208 | return checkAttributeMatchRuleAppliesTo(D, MatchRule); |
| 209 | } |
| 210 | |
| 211 | void AttributeList::getMatchRules( |
| 212 | const LangOptions &LangOpts, |
| 213 | SmallVectorImpl<std::pair<attr::SubjectMatchRule, bool>> &MatchRules) |
| 214 | const { |
| 215 | return getInfo(*this).GetPragmaAttributeMatchRules(MatchRules, LangOpts); |
| 216 | } |
| 217 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 218 | bool AttributeList::diagnoseLangOpts(Sema &S) const { |
| 219 | return getInfo(*this).DiagLangOpts(S, *this); |
| 220 | } |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 221 | |
| 222 | bool AttributeList::isTargetSpecificAttr() const { |
| 223 | return getInfo(*this).IsTargetSpecific; |
| 224 | } |
| 225 | |
| 226 | bool AttributeList::isTypeAttr() const { |
| 227 | return getInfo(*this).IsType; |
| 228 | } |
| 229 | |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 230 | bool AttributeList::isStmtAttr() const { |
| 231 | return getInfo(*this).IsStmt; |
| 232 | } |
| 233 | |
Bob Wilson | 7c73083 | 2015-07-20 22:57:31 +0000 | [diff] [blame] | 234 | bool AttributeList::existsInTarget(const TargetInfo &Target) const { |
| 235 | return getInfo(*this).ExistsInTarget(Target); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 236 | } |
Aaron Ballman | 9a99e0d | 2014-01-20 17:18:35 +0000 | [diff] [blame] | 237 | |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 238 | bool AttributeList::isKnownToGCC() const { |
| 239 | return getInfo(*this).IsKnownToGCC; |
Aaron Ballman | 9a99e0d | 2014-01-20 17:18:35 +0000 | [diff] [blame] | 240 | } |
Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 241 | |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 242 | bool AttributeList::isSupportedByPragmaAttribute() const { |
| 243 | return getInfo(*this).IsSupportedByPragmaAttribute; |
| 244 | } |
| 245 | |
Aaron Ballman | 81cb8cb | 2014-01-24 21:32:49 +0000 | [diff] [blame] | 246 | unsigned AttributeList::getSemanticSpelling() const { |
| 247 | return getInfo(*this).SpellingIndexToSemanticSpelling(*this); |
| 248 | } |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 249 | |
| 250 | bool AttributeList::hasVariadicArg() const { |
| 251 | // If the attribute has the maximum number of optional arguments, we will |
| 252 | // claim that as being variadic. If we someday get an attribute that |
| 253 | // legitimately bumps up against that maximum, we can use another bit to track |
| 254 | // whether it's truly variadic or not. |
| 255 | return getInfo(*this).OptArgs == 15; |
| 256 | } |