blob: dbb31b649045e3a37f9c556bf14dddb9cb8cd00b [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"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000020#include "clang/Sema/SemaInternal.h"
Richard Smith84837d52012-05-03 18:27:39 +000021#include "llvm/ADT/SmallString.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "llvm/ADT/StringSwitch.h"
Steve Naroffb8371e12007-06-09 03:39:29 +000023using namespace clang;
24
Richard Smithfeefaf52013-09-03 18:01:40 +000025IdentifierLoc *IdentifierLoc::create(ASTContext &Ctx, SourceLocation Loc,
26 IdentifierInfo *Ident) {
27 IdentifierLoc *Result = new (Ctx) IdentifierLoc;
28 Result->Loc = Loc;
29 Result->Ident = Ident;
30 return Result;
31}
32
John McCall084e83d2011-03-24 11:26:52 +000033size_t AttributeList::allocated_size() const {
34 if (IsAvailability) return AttributeFactory::AvailabilityAllocSize;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000035 else if (IsTypeTagForDatatype)
36 return AttributeFactory::TypeTagForDatatypeAllocSize;
John McCall5e77d762013-04-16 07:28:30 +000037 else if (IsProperty)
38 return AttributeFactory::PropertyAllocSize;
Aaron Ballman00e99962013-08-31 01:11:41 +000039 return (sizeof(AttributeList) + NumArgs * sizeof(ArgsUnion));
Steve Naroffb8371e12007-06-09 03:39:29 +000040}
Chris Lattnerf18c7432008-02-20 23:14:47 +000041
John McCall084e83d2011-03-24 11:26:52 +000042AttributeFactory::AttributeFactory() {
43 // Go ahead and configure all the inline capacity. This is just a memset.
44 FreeLists.resize(InlineFreeListsCapacity);
45}
46AttributeFactory::~AttributeFactory() {}
47
48static size_t getFreeListIndexForSize(size_t size) {
49 assert(size >= sizeof(AttributeList));
50 assert((size % sizeof(void*)) == 0);
51 return ((size - sizeof(AttributeList)) / sizeof(void*));
52}
53
54void *AttributeFactory::allocate(size_t size) {
55 // Check for a previously reclaimed attribute.
56 size_t index = getFreeListIndexForSize(size);
57 if (index < FreeLists.size()) {
58 if (AttributeList *attr = FreeLists[index]) {
59 FreeLists[index] = attr->NextInPool;
60 return attr;
61 }
62 }
63
64 // Otherwise, allocate something new.
65 return Alloc.Allocate(size, llvm::AlignOf<AttributeFactory>::Alignment);
66}
67
68void AttributeFactory::reclaimPool(AttributeList *cur) {
69 assert(cur && "reclaiming empty pool!");
70 do {
71 // Read this here, because we're going to overwrite NextInPool
72 // when we toss 'cur' into the appropriate queue.
73 AttributeList *next = cur->NextInPool;
74
75 size_t size = cur->allocated_size();
76 size_t freeListIndex = getFreeListIndexForSize(size);
77
78 // Expand FreeLists to the appropriate size, if required.
79 if (freeListIndex >= FreeLists.size())
80 FreeLists.resize(freeListIndex+1);
81
82 // Add 'cur' to the appropriate free-list.
83 cur->NextInPool = FreeLists[freeListIndex];
84 FreeLists[freeListIndex] = cur;
85
86 cur = next;
87 } while (cur);
88}
89
90void AttributePool::takePool(AttributeList *pool) {
91 assert(pool);
92
93 // Fast path: this pool is empty.
94 if (!Head) {
95 Head = pool;
96 return;
97 }
98
99 // Reverse the pool onto the current head. This optimizes for the
100 // pattern of pulling a lot of pools into a single pool.
101 do {
102 AttributeList *next = pool->NextInPool;
103 pool->NextInPool = Head;
104 Head = pool;
105 pool = next;
106 } while (pool);
107}
108
109AttributeList *
110AttributePool::createIntegerAttribute(ASTContext &C, IdentifierInfo *Name,
111 SourceLocation TokLoc, int Arg) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000112 ArgsUnion IArg = IntegerLiteral::Create(C, llvm::APInt(32, (uint64_t) Arg),
John McCall084e83d2011-03-24 11:26:52 +0000113 C.IntTy, TokLoc);
Aaron Ballman00e99962013-08-31 01:11:41 +0000114 return create(Name, TokLoc, 0, TokLoc, &IArg, 1, AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000115}
116
Douglas Gregor377f99b2012-05-02 17:33:51 +0000117#include "clang/Sema/AttrParsedAttrKinds.inc"
118
Richard Smith84837d52012-05-03 18:27:39 +0000119AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name,
Alexis Hunta0e54d42012-06-18 16:13:52 +0000120 const IdentifierInfo *ScopeName,
121 Syntax SyntaxUsed) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000122 StringRef AttrName = Name->getName();
Chris Lattnerf18c7432008-02-20 23:14:47 +0000123
Aaron Ballmanc6988092013-12-11 22:27:44 +0000124 SmallString<64> FullName;
Richard Smith84837d52012-05-03 18:27:39 +0000125 if (ScopeName)
Aaron Ballmanc6988092013-12-11 22:27:44 +0000126 FullName += ScopeName->getName();
127
128 // Normalize the attribute name, __foo__ becomes foo. This is only allowable
129 // for GNU attributes.
130 bool IsGNU = SyntaxUsed == AS_GNU || (SyntaxUsed == AS_CXX11 &&
131 FullName.equals("gnu"));
132 if (IsGNU && AttrName.size() >= 4 && AttrName.startswith("__") &&
133 AttrName.endswith("__"))
134 AttrName = AttrName.slice(2, AttrName.size() - 2);
135
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.
148 StringRef Name = AttrName->getName();
149 StringRef Scope = ScopeName ? ScopeName->getName() : "";
150
151#include "clang/Sema/AttrSpellingListIndex.inc"
152
153}
154
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000155struct ParsedAttrInfo {
156 unsigned NumArgs : 4;
157 unsigned OptArgs : 4;
158 unsigned HasCustomParsing : 1;
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000159 unsigned IsTargetSpecific : 1;
160 unsigned IsType : 1;
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000161
162 bool (*DiagAppertainsToDecl)(Sema &S, const AttributeList &Attr,
163 const Decl *);
Aaron Ballman3aff6332013-12-02 19:30:36 +0000164 bool (*DiagLangOpts)(Sema &S, const AttributeList &Attr);
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000165 bool (*ExistsInTarget)(llvm::Triple T);
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000166};
167
168namespace {
169 #include "clang/Sema/AttrParsedAttrImpl.inc"
170}
171
Benjamin Kramer602623f2013-09-28 15:08:41 +0000172static const ParsedAttrInfo &getInfo(const AttributeList &A) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000173 return AttrInfoMap[A.getKind()];
174}
175
176unsigned AttributeList::getMinArgs() const {
177 return getInfo(*this).NumArgs;
178}
179
180unsigned AttributeList::getMaxArgs() const {
181 return getMinArgs() + getInfo(*this).OptArgs;
182}
183
184bool AttributeList::hasCustomParsing() const {
185 return getInfo(*this).HasCustomParsing;
186}
Aaron Ballman74eeeae2013-11-27 13:27:02 +0000187
188bool AttributeList::diagnoseAppertainsTo(Sema &S, const Decl *D) const {
189 return getInfo(*this).DiagAppertainsToDecl(S, *this, D);
190}
Aaron Ballman3aff6332013-12-02 19:30:36 +0000191
192bool AttributeList::diagnoseLangOpts(Sema &S) const {
193 return getInfo(*this).DiagLangOpts(S, *this);
194}
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000195
196bool AttributeList::isTargetSpecificAttr() const {
197 return getInfo(*this).IsTargetSpecific;
198}
199
200bool AttributeList::isTypeAttr() const {
201 return getInfo(*this).IsType;
202}
203
204bool AttributeList::existsInTarget(llvm::Triple T) const {
205 return getInfo(*this).ExistsInTarget(T);
206}