]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Sema/AttributeList.cpp
Merge compiler-rt release_40 branch r292009.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Sema / AttributeList.cpp
1 //===--- AttributeList.cpp --------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the AttributeList class implementation
11 //
12 //===----------------------------------------------------------------------===//
13
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/Basic/TargetInfo.h"
21 #include "clang/Sema/SemaInternal.h"
22 #include "llvm/ADT/SmallString.h"
23 using namespace clang;
24
25 IdentifierLoc *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
33 size_t AttributeList::allocated_size() const {
34   if (IsAvailability) return AttributeFactory::AvailabilityAllocSize;
35   else if (IsTypeTagForDatatype)
36     return AttributeFactory::TypeTagForDatatypeAllocSize;
37   else if (IsProperty)
38     return AttributeFactory::PropertyAllocSize;
39   return (sizeof(AttributeList) + NumArgs * sizeof(ArgsUnion));
40 }
41
42 AttributeFactory::AttributeFactory() {
43   // Go ahead and configure all the inline capacity.  This is just a memset.
44   FreeLists.resize(InlineFreeListsCapacity);
45 }
46 AttributeFactory::~AttributeFactory() {}
47
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*));
52 }
53
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;
60       return attr;
61     }
62   }
63
64   // Otherwise, allocate something new.
65   return Alloc.Allocate(size, alignof(AttributeFactory));
66 }
67
68 void 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
90 void 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
109 #include "clang/Sema/AttrParsedAttrKinds.inc"
110
111 static StringRef normalizeAttrName(StringRef AttrName, StringRef ScopeName,
112                                    AttributeList::Syntax SyntaxUsed) {
113   // Normalize the attribute name, __foo__ becomes foo. This is only allowable
114   // for GNU attributes.
115   bool IsGNU = SyntaxUsed == AttributeList::AS_GNU ||
116                (SyntaxUsed == AttributeList::AS_CXX11 && ScopeName == "gnu");
117   if (IsGNU && AttrName.size() >= 4 && AttrName.startswith("__") &&
118       AttrName.endswith("__"))
119     AttrName = AttrName.slice(2, AttrName.size() - 2);
120
121   return AttrName;
122 }
123
124 AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name,
125                                            const IdentifierInfo *ScopeName,
126                                            Syntax SyntaxUsed) {
127   StringRef AttrName = Name->getName();
128
129   SmallString<64> FullName;
130   if (ScopeName)
131     FullName += ScopeName->getName();
132
133   AttrName = normalizeAttrName(AttrName, FullName, SyntaxUsed);
134
135   // Ensure that in the case of C++11 attributes, we look for '::foo' if it is
136   // unscoped.
137   if (ScopeName || SyntaxUsed == AS_CXX11)
138     FullName += "::";
139   FullName += AttrName;
140
141   return ::getAttrKind(FullName, SyntaxUsed);
142 }
143
144 unsigned AttributeList::getAttributeSpellingListIndex() const {
145   // Both variables will be used in tablegen generated
146   // attribute spell list index matching code.
147   StringRef Scope = ScopeName ? ScopeName->getName() : "";
148   StringRef Name = normalizeAttrName(AttrName->getName(), Scope,
149                                      (AttributeList::Syntax)SyntaxUsed);
150
151 #include "clang/Sema/AttrSpellingListIndex.inc"
152
153 }
154
155 struct ParsedAttrInfo {
156   unsigned NumArgs : 4;
157   unsigned OptArgs : 4;
158   unsigned HasCustomParsing : 1;
159   unsigned IsTargetSpecific : 1;
160   unsigned IsType : 1;
161   unsigned IsStmt : 1;
162   unsigned IsKnownToGCC : 1;
163
164   bool (*DiagAppertainsToDecl)(Sema &S, const AttributeList &Attr,
165                                const Decl *);
166   bool (*DiagLangOpts)(Sema &S, const AttributeList &Attr);
167   bool (*ExistsInTarget)(const TargetInfo &Target);
168   unsigned (*SpellingIndexToSemanticSpelling)(const AttributeList &Attr);
169 };
170
171 namespace {
172   #include "clang/Sema/AttrParsedAttrImpl.inc"
173 }
174
175 static const ParsedAttrInfo &getInfo(const AttributeList &A) {
176   return AttrInfoMap[A.getKind()];
177 }
178
179 unsigned AttributeList::getMinArgs() const {
180   return getInfo(*this).NumArgs;
181 }
182
183 unsigned AttributeList::getMaxArgs() const {
184   return getMinArgs() + getInfo(*this).OptArgs;
185 }
186
187 bool AttributeList::hasCustomParsing() const {
188   return getInfo(*this).HasCustomParsing;
189 }
190
191 bool AttributeList::diagnoseAppertainsTo(Sema &S, const Decl *D) const {
192   return getInfo(*this).DiagAppertainsToDecl(S, *this, D);
193 }
194
195 bool AttributeList::diagnoseLangOpts(Sema &S) const {
196   return getInfo(*this).DiagLangOpts(S, *this);
197 }
198
199 bool AttributeList::isTargetSpecificAttr() const {
200   return getInfo(*this).IsTargetSpecific;
201 }
202
203 bool AttributeList::isTypeAttr() const {
204   return getInfo(*this).IsType;
205 }
206
207 bool AttributeList::isStmtAttr() const {
208   return getInfo(*this).IsStmt;
209 }
210
211 bool AttributeList::existsInTarget(const TargetInfo &Target) const {
212   return getInfo(*this).ExistsInTarget(Target);
213 }
214
215 bool AttributeList::isKnownToGCC() const {
216   return getInfo(*this).IsKnownToGCC;
217 }
218
219 unsigned AttributeList::getSemanticSpelling() const {
220   return getInfo(*this).SpellingIndexToSemanticSpelling(*this);
221 }
222
223 bool AttributeList::hasVariadicArg() const {
224   // If the attribute has the maximum number of optional arguments, we will
225   // claim that as being variadic. If we someday get an attribute that
226   // legitimately bumps up against that maximum, we can use another bit to track
227   // whether it's truly variadic or not.
228   return getInfo(*this).OptArgs == 15;
229 }