1f4a2713aSLionel Sambuc //===--- AttributeList.cpp --------------------------------------*- C++ -*-===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file defines the AttributeList class implementation
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc
14f4a2713aSLionel Sambuc #include "clang/Sema/AttributeList.h"
15f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
16*0a6a1f1dSLionel Sambuc #include "clang/AST/DeclCXX.h"
17*0a6a1f1dSLionel Sambuc #include "clang/AST/DeclTemplate.h"
18f4a2713aSLionel Sambuc #include "clang/AST/Expr.h"
19f4a2713aSLionel Sambuc #include "clang/Basic/IdentifierTable.h"
20*0a6a1f1dSLionel Sambuc #include "clang/Sema/SemaInternal.h"
21f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
22f4a2713aSLionel Sambuc #include "llvm/ADT/StringSwitch.h"
23f4a2713aSLionel Sambuc using namespace clang;
24f4a2713aSLionel Sambuc
create(ASTContext & Ctx,SourceLocation Loc,IdentifierInfo * Ident)25f4a2713aSLionel Sambuc IdentifierLoc *IdentifierLoc::create(ASTContext &Ctx, SourceLocation Loc,
26f4a2713aSLionel Sambuc IdentifierInfo *Ident) {
27f4a2713aSLionel Sambuc IdentifierLoc *Result = new (Ctx) IdentifierLoc;
28f4a2713aSLionel Sambuc Result->Loc = Loc;
29f4a2713aSLionel Sambuc Result->Ident = Ident;
30f4a2713aSLionel Sambuc return Result;
31f4a2713aSLionel Sambuc }
32f4a2713aSLionel Sambuc
allocated_size() const33f4a2713aSLionel Sambuc size_t AttributeList::allocated_size() const {
34f4a2713aSLionel Sambuc if (IsAvailability) return AttributeFactory::AvailabilityAllocSize;
35f4a2713aSLionel Sambuc else if (IsTypeTagForDatatype)
36f4a2713aSLionel Sambuc return AttributeFactory::TypeTagForDatatypeAllocSize;
37f4a2713aSLionel Sambuc else if (IsProperty)
38f4a2713aSLionel Sambuc return AttributeFactory::PropertyAllocSize;
39f4a2713aSLionel Sambuc return (sizeof(AttributeList) + NumArgs * sizeof(ArgsUnion));
40f4a2713aSLionel Sambuc }
41f4a2713aSLionel Sambuc
AttributeFactory()42f4a2713aSLionel Sambuc AttributeFactory::AttributeFactory() {
43f4a2713aSLionel Sambuc // Go ahead and configure all the inline capacity. This is just a memset.
44f4a2713aSLionel Sambuc FreeLists.resize(InlineFreeListsCapacity);
45f4a2713aSLionel Sambuc }
~AttributeFactory()46f4a2713aSLionel Sambuc AttributeFactory::~AttributeFactory() {}
47f4a2713aSLionel Sambuc
getFreeListIndexForSize(size_t size)48f4a2713aSLionel Sambuc static size_t getFreeListIndexForSize(size_t size) {
49f4a2713aSLionel Sambuc assert(size >= sizeof(AttributeList));
50f4a2713aSLionel Sambuc assert((size % sizeof(void*)) == 0);
51f4a2713aSLionel Sambuc return ((size - sizeof(AttributeList)) / sizeof(void*));
52f4a2713aSLionel Sambuc }
53f4a2713aSLionel Sambuc
allocate(size_t size)54f4a2713aSLionel Sambuc void *AttributeFactory::allocate(size_t size) {
55f4a2713aSLionel Sambuc // Check for a previously reclaimed attribute.
56f4a2713aSLionel Sambuc size_t index = getFreeListIndexForSize(size);
57f4a2713aSLionel Sambuc if (index < FreeLists.size()) {
58f4a2713aSLionel Sambuc if (AttributeList *attr = FreeLists[index]) {
59f4a2713aSLionel Sambuc FreeLists[index] = attr->NextInPool;
60f4a2713aSLionel Sambuc return attr;
61f4a2713aSLionel Sambuc }
62f4a2713aSLionel Sambuc }
63f4a2713aSLionel Sambuc
64f4a2713aSLionel Sambuc // Otherwise, allocate something new.
65f4a2713aSLionel Sambuc return Alloc.Allocate(size, llvm::AlignOf<AttributeFactory>::Alignment);
66f4a2713aSLionel Sambuc }
67f4a2713aSLionel Sambuc
reclaimPool(AttributeList * cur)68f4a2713aSLionel Sambuc void AttributeFactory::reclaimPool(AttributeList *cur) {
69f4a2713aSLionel Sambuc assert(cur && "reclaiming empty pool!");
70f4a2713aSLionel Sambuc do {
71f4a2713aSLionel Sambuc // Read this here, because we're going to overwrite NextInPool
72f4a2713aSLionel Sambuc // when we toss 'cur' into the appropriate queue.
73f4a2713aSLionel Sambuc AttributeList *next = cur->NextInPool;
74f4a2713aSLionel Sambuc
75f4a2713aSLionel Sambuc size_t size = cur->allocated_size();
76f4a2713aSLionel Sambuc size_t freeListIndex = getFreeListIndexForSize(size);
77f4a2713aSLionel Sambuc
78f4a2713aSLionel Sambuc // Expand FreeLists to the appropriate size, if required.
79f4a2713aSLionel Sambuc if (freeListIndex >= FreeLists.size())
80f4a2713aSLionel Sambuc FreeLists.resize(freeListIndex+1);
81f4a2713aSLionel Sambuc
82f4a2713aSLionel Sambuc // Add 'cur' to the appropriate free-list.
83f4a2713aSLionel Sambuc cur->NextInPool = FreeLists[freeListIndex];
84f4a2713aSLionel Sambuc FreeLists[freeListIndex] = cur;
85f4a2713aSLionel Sambuc
86f4a2713aSLionel Sambuc cur = next;
87f4a2713aSLionel Sambuc } while (cur);
88f4a2713aSLionel Sambuc }
89f4a2713aSLionel Sambuc
takePool(AttributeList * pool)90f4a2713aSLionel Sambuc void AttributePool::takePool(AttributeList *pool) {
91f4a2713aSLionel Sambuc assert(pool);
92f4a2713aSLionel Sambuc
93f4a2713aSLionel Sambuc // Fast path: this pool is empty.
94f4a2713aSLionel Sambuc if (!Head) {
95f4a2713aSLionel Sambuc Head = pool;
96f4a2713aSLionel Sambuc return;
97f4a2713aSLionel Sambuc }
98f4a2713aSLionel Sambuc
99f4a2713aSLionel Sambuc // Reverse the pool onto the current head. This optimizes for the
100f4a2713aSLionel Sambuc // pattern of pulling a lot of pools into a single pool.
101f4a2713aSLionel Sambuc do {
102f4a2713aSLionel Sambuc AttributeList *next = pool->NextInPool;
103f4a2713aSLionel Sambuc pool->NextInPool = Head;
104f4a2713aSLionel Sambuc Head = pool;
105f4a2713aSLionel Sambuc pool = next;
106f4a2713aSLionel Sambuc } while (pool);
107f4a2713aSLionel Sambuc }
108f4a2713aSLionel Sambuc
109f4a2713aSLionel Sambuc #include "clang/Sema/AttrParsedAttrKinds.inc"
110f4a2713aSLionel Sambuc
getKind(const IdentifierInfo * Name,const IdentifierInfo * ScopeName,Syntax SyntaxUsed)111f4a2713aSLionel Sambuc AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name,
112f4a2713aSLionel Sambuc const IdentifierInfo *ScopeName,
113f4a2713aSLionel Sambuc Syntax SyntaxUsed) {
114f4a2713aSLionel Sambuc StringRef AttrName = Name->getName();
115f4a2713aSLionel Sambuc
116*0a6a1f1dSLionel Sambuc SmallString<64> FullName;
117f4a2713aSLionel Sambuc if (ScopeName)
118*0a6a1f1dSLionel Sambuc FullName += ScopeName->getName();
119*0a6a1f1dSLionel Sambuc
120*0a6a1f1dSLionel Sambuc // Normalize the attribute name, __foo__ becomes foo. This is only allowable
121*0a6a1f1dSLionel Sambuc // for GNU attributes.
122*0a6a1f1dSLionel Sambuc bool IsGNU = SyntaxUsed == AS_GNU || (SyntaxUsed == AS_CXX11 &&
123*0a6a1f1dSLionel Sambuc FullName == "gnu");
124*0a6a1f1dSLionel Sambuc if (IsGNU && AttrName.size() >= 4 && AttrName.startswith("__") &&
125*0a6a1f1dSLionel Sambuc AttrName.endswith("__"))
126*0a6a1f1dSLionel Sambuc AttrName = AttrName.slice(2, AttrName.size() - 2);
127*0a6a1f1dSLionel Sambuc
128f4a2713aSLionel Sambuc // Ensure that in the case of C++11 attributes, we look for '::foo' if it is
129f4a2713aSLionel Sambuc // unscoped.
130f4a2713aSLionel Sambuc if (ScopeName || SyntaxUsed == AS_CXX11)
131*0a6a1f1dSLionel Sambuc FullName += "::";
132*0a6a1f1dSLionel Sambuc FullName += AttrName;
133f4a2713aSLionel Sambuc
134*0a6a1f1dSLionel Sambuc return ::getAttrKind(FullName, SyntaxUsed);
135f4a2713aSLionel Sambuc }
136f4a2713aSLionel Sambuc
getAttributeSpellingListIndex() const137f4a2713aSLionel Sambuc unsigned AttributeList::getAttributeSpellingListIndex() const {
138f4a2713aSLionel Sambuc // Both variables will be used in tablegen generated
139f4a2713aSLionel Sambuc // attribute spell list index matching code.
140f4a2713aSLionel Sambuc StringRef Name = AttrName->getName();
141f4a2713aSLionel Sambuc StringRef Scope = ScopeName ? ScopeName->getName() : "";
142f4a2713aSLionel Sambuc
143f4a2713aSLionel Sambuc #include "clang/Sema/AttrSpellingListIndex.inc"
144f4a2713aSLionel Sambuc
145f4a2713aSLionel Sambuc }
146f4a2713aSLionel Sambuc
147f4a2713aSLionel Sambuc struct ParsedAttrInfo {
148f4a2713aSLionel Sambuc unsigned NumArgs : 4;
149f4a2713aSLionel Sambuc unsigned OptArgs : 4;
150f4a2713aSLionel Sambuc unsigned HasCustomParsing : 1;
151*0a6a1f1dSLionel Sambuc unsigned IsTargetSpecific : 1;
152*0a6a1f1dSLionel Sambuc unsigned IsType : 1;
153*0a6a1f1dSLionel Sambuc unsigned IsKnownToGCC : 1;
154*0a6a1f1dSLionel Sambuc
155*0a6a1f1dSLionel Sambuc bool (*DiagAppertainsToDecl)(Sema &S, const AttributeList &Attr,
156*0a6a1f1dSLionel Sambuc const Decl *);
157*0a6a1f1dSLionel Sambuc bool (*DiagLangOpts)(Sema &S, const AttributeList &Attr);
158*0a6a1f1dSLionel Sambuc bool (*ExistsInTarget)(const llvm::Triple &T);
159*0a6a1f1dSLionel Sambuc unsigned (*SpellingIndexToSemanticSpelling)(const AttributeList &Attr);
160f4a2713aSLionel Sambuc };
161f4a2713aSLionel Sambuc
162f4a2713aSLionel Sambuc namespace {
163f4a2713aSLionel Sambuc #include "clang/Sema/AttrParsedAttrImpl.inc"
164f4a2713aSLionel Sambuc }
165f4a2713aSLionel Sambuc
getInfo(const AttributeList & A)166f4a2713aSLionel Sambuc static const ParsedAttrInfo &getInfo(const AttributeList &A) {
167f4a2713aSLionel Sambuc return AttrInfoMap[A.getKind()];
168f4a2713aSLionel Sambuc }
169f4a2713aSLionel Sambuc
getMinArgs() const170f4a2713aSLionel Sambuc unsigned AttributeList::getMinArgs() const {
171f4a2713aSLionel Sambuc return getInfo(*this).NumArgs;
172f4a2713aSLionel Sambuc }
173f4a2713aSLionel Sambuc
getMaxArgs() const174f4a2713aSLionel Sambuc unsigned AttributeList::getMaxArgs() const {
175f4a2713aSLionel Sambuc return getMinArgs() + getInfo(*this).OptArgs;
176f4a2713aSLionel Sambuc }
177f4a2713aSLionel Sambuc
hasCustomParsing() const178f4a2713aSLionel Sambuc bool AttributeList::hasCustomParsing() const {
179f4a2713aSLionel Sambuc return getInfo(*this).HasCustomParsing;
180f4a2713aSLionel Sambuc }
181*0a6a1f1dSLionel Sambuc
diagnoseAppertainsTo(Sema & S,const Decl * D) const182*0a6a1f1dSLionel Sambuc bool AttributeList::diagnoseAppertainsTo(Sema &S, const Decl *D) const {
183*0a6a1f1dSLionel Sambuc return getInfo(*this).DiagAppertainsToDecl(S, *this, D);
184*0a6a1f1dSLionel Sambuc }
185*0a6a1f1dSLionel Sambuc
diagnoseLangOpts(Sema & S) const186*0a6a1f1dSLionel Sambuc bool AttributeList::diagnoseLangOpts(Sema &S) const {
187*0a6a1f1dSLionel Sambuc return getInfo(*this).DiagLangOpts(S, *this);
188*0a6a1f1dSLionel Sambuc }
189*0a6a1f1dSLionel Sambuc
isTargetSpecificAttr() const190*0a6a1f1dSLionel Sambuc bool AttributeList::isTargetSpecificAttr() const {
191*0a6a1f1dSLionel Sambuc return getInfo(*this).IsTargetSpecific;
192*0a6a1f1dSLionel Sambuc }
193*0a6a1f1dSLionel Sambuc
isTypeAttr() const194*0a6a1f1dSLionel Sambuc bool AttributeList::isTypeAttr() const {
195*0a6a1f1dSLionel Sambuc return getInfo(*this).IsType;
196*0a6a1f1dSLionel Sambuc }
197*0a6a1f1dSLionel Sambuc
existsInTarget(const llvm::Triple & T) const198*0a6a1f1dSLionel Sambuc bool AttributeList::existsInTarget(const llvm::Triple &T) const {
199*0a6a1f1dSLionel Sambuc return getInfo(*this).ExistsInTarget(T);
200*0a6a1f1dSLionel Sambuc }
201*0a6a1f1dSLionel Sambuc
isKnownToGCC() const202*0a6a1f1dSLionel Sambuc bool AttributeList::isKnownToGCC() const {
203*0a6a1f1dSLionel Sambuc return getInfo(*this).IsKnownToGCC;
204*0a6a1f1dSLionel Sambuc }
205*0a6a1f1dSLionel Sambuc
getSemanticSpelling() const206*0a6a1f1dSLionel Sambuc unsigned AttributeList::getSemanticSpelling() const {
207*0a6a1f1dSLionel Sambuc return getInfo(*this).SpellingIndexToSemanticSpelling(*this);
208*0a6a1f1dSLionel Sambuc }
209*0a6a1f1dSLionel Sambuc
hasVariadicArg() const210*0a6a1f1dSLionel Sambuc bool AttributeList::hasVariadicArg() const {
211*0a6a1f1dSLionel Sambuc // If the attribute has the maximum number of optional arguments, we will
212*0a6a1f1dSLionel Sambuc // claim that as being variadic. If we someday get an attribute that
213*0a6a1f1dSLionel Sambuc // legitimately bumps up against that maximum, we can use another bit to track
214*0a6a1f1dSLionel Sambuc // whether it's truly variadic or not.
215*0a6a1f1dSLionel Sambuc return getInfo(*this).OptArgs == 15;
216*0a6a1f1dSLionel Sambuc }
217