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