1 //===--- AttributeList.cpp --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the AttributeList class implementation
12 //===----------------------------------------------------------------------===//
14 #include "clang/Sema/AttributeList.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/DeclCXX.h"
17 #include "clang/AST/DeclTemplate.h"
18 #include "clang/AST/Expr.h"
19 #include "clang/Basic/IdentifierTable.h"
20 #include "clang/Sema/SemaInternal.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/StringSwitch.h"
23 using namespace clang;
25 IdentifierLoc *IdentifierLoc::create(ASTContext &Ctx, SourceLocation Loc,
26 IdentifierInfo *Ident) {
27 IdentifierLoc *Result = new (Ctx) IdentifierLoc;
29 Result->Ident = Ident;
33 size_t AttributeList::allocated_size() const {
34 if (IsAvailability) return AttributeFactory::AvailabilityAllocSize;
35 else if (IsTypeTagForDatatype)
36 return AttributeFactory::TypeTagForDatatypeAllocSize;
38 return AttributeFactory::PropertyAllocSize;
39 return (sizeof(AttributeList) + NumArgs * sizeof(ArgsUnion));
42 AttributeFactory::AttributeFactory() {
43 // Go ahead and configure all the inline capacity. This is just a memset.
44 FreeLists.resize(InlineFreeListsCapacity);
46 AttributeFactory::~AttributeFactory() {}
48 static size_t getFreeListIndexForSize(size_t size) {
49 assert(size >= sizeof(AttributeList));
50 assert((size % sizeof(void*)) == 0);
51 return ((size - sizeof(AttributeList)) / sizeof(void*));
54 void *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;
64 // Otherwise, allocate something new.
65 return Alloc.Allocate(size, llvm::AlignOf<AttributeFactory>::Alignment);
68 void AttributeFactory::reclaimPool(AttributeList *cur) {
69 assert(cur && "reclaiming empty pool!");
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;
75 size_t size = cur->allocated_size();
76 size_t freeListIndex = getFreeListIndexForSize(size);
78 // Expand FreeLists to the appropriate size, if required.
79 if (freeListIndex >= FreeLists.size())
80 FreeLists.resize(freeListIndex+1);
82 // Add 'cur' to the appropriate free-list.
83 cur->NextInPool = FreeLists[freeListIndex];
84 FreeLists[freeListIndex] = cur;
90 void AttributePool::takePool(AttributeList *pool) {
93 // Fast path: this pool is empty.
99 // Reverse the pool onto the current head. This optimizes for the
100 // pattern of pulling a lot of pools into a single pool.
102 AttributeList *next = pool->NextInPool;
103 pool->NextInPool = Head;
109 #include "clang/Sema/AttrParsedAttrKinds.inc"
111 AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name,
112 const IdentifierInfo *ScopeName,
114 StringRef AttrName = Name->getName();
116 SmallString<64> FullName;
118 FullName += ScopeName->getName();
120 // Normalize the attribute name, __foo__ becomes foo. This is only allowable
121 // for GNU attributes.
122 bool IsGNU = SyntaxUsed == AS_GNU || (SyntaxUsed == AS_CXX11 &&
124 if (IsGNU && AttrName.size() >= 4 && AttrName.startswith("__") &&
125 AttrName.endswith("__"))
126 AttrName = AttrName.slice(2, AttrName.size() - 2);
128 // Ensure that in the case of C++11 attributes, we look for '::foo' if it is
130 if (ScopeName || SyntaxUsed == AS_CXX11)
132 FullName += AttrName;
134 return ::getAttrKind(FullName, SyntaxUsed);
137 unsigned AttributeList::getAttributeSpellingListIndex() const {
138 // Both variables will be used in tablegen generated
139 // attribute spell list index matching code.
140 StringRef Name = AttrName->getName();
141 StringRef Scope = ScopeName ? ScopeName->getName() : "";
143 #include "clang/Sema/AttrSpellingListIndex.inc"
147 struct ParsedAttrInfo {
148 unsigned NumArgs : 4;
149 unsigned OptArgs : 4;
150 unsigned HasCustomParsing : 1;
151 unsigned IsTargetSpecific : 1;
153 unsigned IsKnownToGCC : 1;
155 bool (*DiagAppertainsToDecl)(Sema &S, const AttributeList &Attr,
157 bool (*DiagLangOpts)(Sema &S, const AttributeList &Attr);
158 bool (*ExistsInTarget)(const llvm::Triple &T);
159 unsigned (*SpellingIndexToSemanticSpelling)(const AttributeList &Attr);
163 #include "clang/Sema/AttrParsedAttrImpl.inc"
166 static const ParsedAttrInfo &getInfo(const AttributeList &A) {
167 return AttrInfoMap[A.getKind()];
170 unsigned AttributeList::getMinArgs() const {
171 return getInfo(*this).NumArgs;
174 unsigned AttributeList::getMaxArgs() const {
175 return getMinArgs() + getInfo(*this).OptArgs;
178 bool AttributeList::hasCustomParsing() const {
179 return getInfo(*this).HasCustomParsing;
182 bool AttributeList::diagnoseAppertainsTo(Sema &S, const Decl *D) const {
183 return getInfo(*this).DiagAppertainsToDecl(S, *this, D);
186 bool AttributeList::diagnoseLangOpts(Sema &S) const {
187 return getInfo(*this).DiagLangOpts(S, *this);
190 bool AttributeList::isTargetSpecificAttr() const {
191 return getInfo(*this).IsTargetSpecific;
194 bool AttributeList::isTypeAttr() const {
195 return getInfo(*this).IsType;
198 bool AttributeList::existsInTarget(const llvm::Triple &T) const {
199 return getInfo(*this).ExistsInTarget(T);
202 bool AttributeList::isKnownToGCC() const {
203 return getInfo(*this).IsKnownToGCC;
206 unsigned AttributeList::getSemanticSpelling() const {
207 return getInfo(*this).SpellingIndexToSemanticSpelling(*this);
210 bool AttributeList::hasVariadicArg() const {
211 // If the attribute has the maximum number of optional arguments, we will
212 // claim that as being variadic. If we someday get an attribute that
213 // legitimately bumps up against that maximum, we can use another bit to track
214 // whether it's truly variadic or not.
215 return getInfo(*this).OptArgs == 15;