blob: cae9393f9f3aae4b9914a7feecae373cae9db4d9 [file] [log] [blame]
Steve Naroffb8371e12007-06-09 03:39:29 +00001//===--- AttributeList.cpp --------------------------------------*- C++ -*-===//
2//
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"
Aaron Ballman74eeeae2013-11-27 13:27:02 +000016#include "clang/AST/DeclCXX.h"
17#include "clang/AST/DeclTemplate.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000018#include "clang/AST/Expr.h"
Chris Lattner7e0fe442009-04-11 18:48:18 +000019#include "clang/Basic/IdentifierTable.h"
Bob Wilson7c730832015-07-20 22:57:31 +000020#include "clang/Basic/TargetInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000021#include "clang/Sema/SemaInternal.h"
Richard Smith84837d52012-05-03 18:27:39 +000022#include "llvm/ADT/SmallString.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "llvm/ADT/StringSwitch.h"
Steve Naroffb8371e12007-06-09 03:39:29 +000024using namespace clang;
25
Richard Smithfeefaf52013-09-03 18:01:40 +000026IdentifierLoc *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 McCall084e83d2011-03-24 11:26:52 +000034size_t AttributeList::allocated_size() const {
35 if (IsAvailability) return AttributeFactory::AvailabilityAllocSize;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000036 else if (IsTypeTagForDatatype)
37 return AttributeFactory::TypeTagForDatatypeAllocSize;
John McCall5e77d762013-04-16 07:28:30 +000038 else if (IsProperty)
39 return AttributeFactory::PropertyAllocSize;
Aaron Ballman00e99962013-08-31 01:11:41 +000040 return (sizeof(AttributeList) + NumArgs * sizeof(ArgsUnion));
Steve Naroffb8371e12007-06-09 03:39:29 +000041}
Chris Lattnerf18c7432008-02-20 23:14:47 +000042
John McCall084e83d2011-03-24 11:26:52 +000043AttributeFactory::AttributeFactory() {
44 // Go ahead and configure all the inline capacity. This is just a memset.
45 FreeLists.resize(InlineFreeListsCapacity);
46}
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000047AttributeFactory::~AttributeFactory() {}
John McCall084e83d2011-03-24 11:26:52 +000048
49static 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
55void *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.
66 return Alloc.Allocate(size, llvm::AlignOf<AttributeFactory>::Alignment);
67}
68
69void 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
91void 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 Gregor377f99b2012-05-02 17:33:51 +0000110#include "clang/Sema/AttrParsedAttrKinds.inc"
111
David Majnemera94e7b62015-08-25 16:44:38 +0000112static 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 ||
117 (SyntaxUsed == AttributeList::AS_CXX11 && ScopeName == "gnu");
118 if (IsGNU && AttrName.size() >= 4 && AttrName.startswith("__") &&
119 AttrName.endswith("__"))
120 AttrName = AttrName.slice(2, AttrName.size() - 2);
121
122 return AttrName;
123}
124
Richard Smith84837d52012-05-03 18:27:39 +0000125AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name,
Alexis Hunta0e54d42012-06-18 16:13:52 +0000126 const IdentifierInfo *ScopeName,
127 Syntax SyntaxUsed) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000128 StringRef AttrName = Name->getName();
Chris Lattnerf18c7432008-02-20 23:14:47 +0000129
Aaron Ballmanc6988092013-12-11 22:27:44 +0000130 SmallString<64> FullName;
Richard Smith84837d52012-05-03 18:27:39 +0000131 if (ScopeName)
Aaron Ballmanc6988092013-12-11 22:27:44 +0000132 FullName += ScopeName->getName();
133
David Majnemera94e7b62015-08-25 16:44:38 +0000134 AttrName = normalizeAttrName(AttrName, FullName, SyntaxUsed);
Aaron Ballmanc6988092013-12-11 22:27:44 +0000135
Alexis Hunta0e54d42012-06-18 16:13:52 +0000136 // Ensure that in the case of C++11 attributes, we look for '::foo' if it is
137 // unscoped.
138 if (ScopeName || SyntaxUsed == AS_CXX11)
Aaron Ballmanc6988092013-12-11 22:27:44 +0000139 FullName += "::";
140 FullName += AttrName;
Richard Smith84837d52012-05-03 18:27:39 +0000141
Aaron Ballman09e98ff2014-01-13 21:42:39 +0000142 return ::getAttrKind(FullName, SyntaxUsed);
Chris Lattnerf18c7432008-02-20 23:14:47 +0000143}
Michael Han99315932013-01-24 16:46:58 +0000144
145unsigned AttributeList::getAttributeSpellingListIndex() const {
146 // Both variables will be used in tablegen generated
147 // attribute spell list index matching code.
Michael Han99315932013-01-24 16:46:58 +0000148 StringRef Scope = ScopeName ? ScopeName->getName() : "";
David Majnemera94e7b62015-08-25 16:44:38 +0000149 StringRef Name = normalizeAttrName(AttrName->getName(), Scope,
150 (AttributeList::Syntax)SyntaxUsed);
Michael Han99315932013-01-24 16:46:58 +0000151
152#include "clang/Sema/AttrSpellingListIndex.inc"
153
154}
155
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000156struct ParsedAttrInfo {
157 unsigned NumArgs : 4;
158 unsigned OptArgs : 4;
159 unsigned HasCustomParsing : 1;
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000160 unsigned IsTargetSpecific : 1;
161 unsigned IsType : 1;
Richard Smith4f902c72016-03-08 00:32:55 +0000162 unsigned IsStmt : 1;
Aaron Ballmanc669cc02014-01-27 22:10:04 +0000163 unsigned IsKnownToGCC : 1;
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000164
165 bool (*DiagAppertainsToDecl)(Sema &S, const AttributeList &Attr,
166 const Decl *);
Aaron Ballman3aff6332013-12-02 19:30:36 +0000167 bool (*DiagLangOpts)(Sema &S, const AttributeList &Attr);
Bob Wilson7c730832015-07-20 22:57:31 +0000168 bool (*ExistsInTarget)(const TargetInfo &Target);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +0000169 unsigned (*SpellingIndexToSemanticSpelling)(const AttributeList &Attr);
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000170};
171
172namespace {
173 #include "clang/Sema/AttrParsedAttrImpl.inc"
174}
175
Benjamin Kramer602623f2013-09-28 15:08:41 +0000176static const ParsedAttrInfo &getInfo(const AttributeList &A) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000177 return AttrInfoMap[A.getKind()];
178}
179
180unsigned AttributeList::getMinArgs() const {
181 return getInfo(*this).NumArgs;
182}
183
184unsigned AttributeList::getMaxArgs() const {
185 return getMinArgs() + getInfo(*this).OptArgs;
186}
187
188bool AttributeList::hasCustomParsing() const {
189 return getInfo(*this).HasCustomParsing;
190}
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000191
192bool AttributeList::diagnoseAppertainsTo(Sema &S, const Decl *D) const {
193 return getInfo(*this).DiagAppertainsToDecl(S, *this, D);
194}
Aaron Ballman3aff6332013-12-02 19:30:36 +0000195
196bool AttributeList::diagnoseLangOpts(Sema &S) const {
197 return getInfo(*this).DiagLangOpts(S, *this);
198}
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000199
200bool AttributeList::isTargetSpecificAttr() const {
201 return getInfo(*this).IsTargetSpecific;
202}
203
204bool AttributeList::isTypeAttr() const {
205 return getInfo(*this).IsType;
206}
207
Richard Smith4f902c72016-03-08 00:32:55 +0000208bool AttributeList::isStmtAttr() const {
209 return getInfo(*this).IsStmt;
210}
211
Bob Wilson7c730832015-07-20 22:57:31 +0000212bool AttributeList::existsInTarget(const TargetInfo &Target) const {
213 return getInfo(*this).ExistsInTarget(Target);
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000214}
Aaron Ballman9a99e0d2014-01-20 17:18:35 +0000215
Aaron Ballmanc669cc02014-01-27 22:10:04 +0000216bool AttributeList::isKnownToGCC() const {
217 return getInfo(*this).IsKnownToGCC;
Aaron Ballman9a99e0d2014-01-20 17:18:35 +0000218}
Aaron Ballman81cb8cb2014-01-24 21:32:49 +0000219
220unsigned AttributeList::getSemanticSpelling() const {
221 return getInfo(*this).SpellingIndexToSemanticSpelling(*this);
222}
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000223
224bool AttributeList::hasVariadicArg() const {
225 // If the attribute has the maximum number of optional arguments, we will
226 // claim that as being variadic. If we someday get an attribute that
227 // legitimately bumps up against that maximum, we can use another bit to track
228 // whether it's truly variadic or not.
229 return getInfo(*this).OptArgs == 15;
230}