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