blob: bdb91c7e39c7a2841a4cf780fb538258503e6ea0 [file] [log] [blame]
Eugene Zelenko711964d2018-02-20 02:16:28 +00001//===- AttributeList.cpp --------------------------------------------------===//
Steve Naroffb8371e12007-06-09 03:39:29 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Steve Naroffb8371e12007-06-09 03:39:29 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the AttributeList class implementation
11//
12//===----------------------------------------------------------------------===//
13
John McCall8b0666c2010-08-20 18:27:03 +000014#include "clang/Sema/AttributeList.h"
Benjamin Kramer1ea8e092012-07-04 17:04:04 +000015#include "clang/AST/ASTContext.h"
Alex Lorenz9e7bf162017-04-18 14:33:39 +000016#include "clang/Basic/AttrSubjectMatchRules.h"
Chris Lattner7e0fe442009-04-11 18:48:18 +000017#include "clang/Basic/IdentifierTable.h"
Bob Wilson7c730832015-07-20 22:57:31 +000018#include "clang/Basic/TargetInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000019#include "clang/Sema/SemaInternal.h"
Richard Smith84837d52012-05-03 18:27:39 +000020#include "llvm/ADT/SmallString.h"
Eugene Zelenko711964d2018-02-20 02:16:28 +000021#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/StringRef.h"
23#include <cassert>
24#include <cstddef>
25#include <utility>
26
Steve Naroffb8371e12007-06-09 03:39:29 +000027using namespace clang;
28
Richard Smithfeefaf52013-09-03 18:01:40 +000029IdentifierLoc *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 McCall084e83d2011-03-24 11:26:52 +000037size_t AttributeList::allocated_size() const {
38 if (IsAvailability) return AttributeFactory::AvailabilityAllocSize;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000039 else if (IsTypeTagForDatatype)
40 return AttributeFactory::TypeTagForDatatypeAllocSize;
John McCall5e77d762013-04-16 07:28:30 +000041 else if (IsProperty)
42 return AttributeFactory::PropertyAllocSize;
Aaron Ballman00e99962013-08-31 01:11:41 +000043 return (sizeof(AttributeList) + NumArgs * sizeof(ArgsUnion));
Steve Naroffb8371e12007-06-09 03:39:29 +000044}
Chris Lattnerf18c7432008-02-20 23:14:47 +000045
John McCall084e83d2011-03-24 11:26:52 +000046AttributeFactory::AttributeFactory() {
47 // Go ahead and configure all the inline capacity. This is just a memset.
48 FreeLists.resize(InlineFreeListsCapacity);
49}
Eugene Zelenko711964d2018-02-20 02:16:28 +000050AttributeFactory::~AttributeFactory() = default;
John McCall084e83d2011-03-24 11:26:52 +000051
52static 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
58void *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 Kramerc3f89252016-10-20 14:27:22 +000069 return Alloc.Allocate(size, alignof(AttributeFactory));
John McCall084e83d2011-03-24 11:26:52 +000070}
71
72void 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
94void 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 Gregor377f99b2012-05-02 17:33:51 +0000113#include "clang/Sema/AttrParsedAttrKinds.inc"
114
David Majnemera94e7b62015-08-25 16:44:38 +0000115static 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 Ballman606093a2017-10-15 15:01:42 +0000120 ((SyntaxUsed == AttributeList::AS_CXX11 ||
121 SyntaxUsed == AttributeList::AS_C2x) && ScopeName == "gnu");
David Majnemera94e7b62015-08-25 16:44:38 +0000122 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 Smith84837d52012-05-03 18:27:39 +0000129AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name,
Alexis Hunta0e54d42012-06-18 16:13:52 +0000130 const IdentifierInfo *ScopeName,
131 Syntax SyntaxUsed) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000132 StringRef AttrName = Name->getName();
Chris Lattnerf18c7432008-02-20 23:14:47 +0000133
Aaron Ballmanc6988092013-12-11 22:27:44 +0000134 SmallString<64> FullName;
Richard Smith84837d52012-05-03 18:27:39 +0000135 if (ScopeName)
Aaron Ballmanc6988092013-12-11 22:27:44 +0000136 FullName += ScopeName->getName();
137
David Majnemera94e7b62015-08-25 16:44:38 +0000138 AttrName = normalizeAttrName(AttrName, FullName, SyntaxUsed);
Aaron Ballmanc6988092013-12-11 22:27:44 +0000139
Alexis Hunta0e54d42012-06-18 16:13:52 +0000140 // Ensure that in the case of C++11 attributes, we look for '::foo' if it is
141 // unscoped.
Aaron Ballman606093a2017-10-15 15:01:42 +0000142 if (ScopeName || SyntaxUsed == AS_CXX11 || SyntaxUsed == AS_C2x)
Aaron Ballmanc6988092013-12-11 22:27:44 +0000143 FullName += "::";
144 FullName += AttrName;
Richard Smith84837d52012-05-03 18:27:39 +0000145
Aaron Ballman09e98ff2014-01-13 21:42:39 +0000146 return ::getAttrKind(FullName, SyntaxUsed);
Chris Lattnerf18c7432008-02-20 23:14:47 +0000147}
Michael Han99315932013-01-24 16:46:58 +0000148
149unsigned AttributeList::getAttributeSpellingListIndex() const {
150 // Both variables will be used in tablegen generated
151 // attribute spell list index matching code.
Michael Han99315932013-01-24 16:46:58 +0000152 StringRef Scope = ScopeName ? ScopeName->getName() : "";
David Majnemera94e7b62015-08-25 16:44:38 +0000153 StringRef Name = normalizeAttrName(AttrName->getName(), Scope,
154 (AttributeList::Syntax)SyntaxUsed);
Michael Han99315932013-01-24 16:46:58 +0000155
156#include "clang/Sema/AttrSpellingListIndex.inc"
157
158}
159
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000160struct ParsedAttrInfo {
161 unsigned NumArgs : 4;
162 unsigned OptArgs : 4;
163 unsigned HasCustomParsing : 1;
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000164 unsigned IsTargetSpecific : 1;
165 unsigned IsType : 1;
Richard Smith4f902c72016-03-08 00:32:55 +0000166 unsigned IsStmt : 1;
Aaron Ballmanc669cc02014-01-27 22:10:04 +0000167 unsigned IsKnownToGCC : 1;
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000168 unsigned IsSupportedByPragmaAttribute : 1;
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000169
170 bool (*DiagAppertainsToDecl)(Sema &S, const AttributeList &Attr,
171 const Decl *);
Aaron Ballman3aff6332013-12-02 19:30:36 +0000172 bool (*DiagLangOpts)(Sema &S, const AttributeList &Attr);
Bob Wilson7c730832015-07-20 22:57:31 +0000173 bool (*ExistsInTarget)(const TargetInfo &Target);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +0000174 unsigned (*SpellingIndexToSemanticSpelling)(const AttributeList &Attr);
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000175 void (*GetPragmaAttributeMatchRules)(
176 llvm::SmallVectorImpl<std::pair<attr::SubjectMatchRule, bool>> &Rules,
177 const LangOptions &LangOpts);
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000178};
179
180namespace {
Eugene Zelenko711964d2018-02-20 02:16:28 +0000181
182#include "clang/Sema/AttrParsedAttrImpl.inc"
183
184} // namespace
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000185
Benjamin Kramer602623f2013-09-28 15:08:41 +0000186static const ParsedAttrInfo &getInfo(const AttributeList &A) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000187 return AttrInfoMap[A.getKind()];
188}
189
190unsigned AttributeList::getMinArgs() const {
191 return getInfo(*this).NumArgs;
192}
193
194unsigned AttributeList::getMaxArgs() const {
195 return getMinArgs() + getInfo(*this).OptArgs;
196}
197
198bool AttributeList::hasCustomParsing() const {
199 return getInfo(*this).HasCustomParsing;
200}
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000201
202bool AttributeList::diagnoseAppertainsTo(Sema &S, const Decl *D) const {
203 return getInfo(*this).DiagAppertainsToDecl(S, *this, D);
204}
Aaron Ballman3aff6332013-12-02 19:30:36 +0000205
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000206bool AttributeList::appliesToDecl(const Decl *D,
207 attr::SubjectMatchRule MatchRule) const {
208 return checkAttributeMatchRuleAppliesTo(D, MatchRule);
209}
210
211void 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 Ballman3aff6332013-12-02 19:30:36 +0000218bool AttributeList::diagnoseLangOpts(Sema &S) const {
219 return getInfo(*this).DiagLangOpts(S, *this);
220}
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000221
222bool AttributeList::isTargetSpecificAttr() const {
223 return getInfo(*this).IsTargetSpecific;
224}
225
226bool AttributeList::isTypeAttr() const {
227 return getInfo(*this).IsType;
228}
229
Richard Smith4f902c72016-03-08 00:32:55 +0000230bool AttributeList::isStmtAttr() const {
231 return getInfo(*this).IsStmt;
232}
233
Bob Wilson7c730832015-07-20 22:57:31 +0000234bool AttributeList::existsInTarget(const TargetInfo &Target) const {
235 return getInfo(*this).ExistsInTarget(Target);
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000236}
Aaron Ballman9a99e0d2014-01-20 17:18:35 +0000237
Aaron Ballmanc669cc02014-01-27 22:10:04 +0000238bool AttributeList::isKnownToGCC() const {
239 return getInfo(*this).IsKnownToGCC;
Aaron Ballman9a99e0d2014-01-20 17:18:35 +0000240}
Aaron Ballman81cb8cb2014-01-24 21:32:49 +0000241
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000242bool AttributeList::isSupportedByPragmaAttribute() const {
243 return getInfo(*this).IsSupportedByPragmaAttribute;
244}
245
Aaron Ballman81cb8cb2014-01-24 21:32:49 +0000246unsigned AttributeList::getSemanticSpelling() const {
247 return getInfo(*this).SpellingIndexToSemanticSpelling(*this);
248}
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000249
250bool 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}