clang API Documentation
00001 //===--- AttributeList.cpp --------------------------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the AttributeList class implementation 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "clang/Sema/AttributeList.h" 00015 #include "clang/AST/ASTContext.h" 00016 #include "clang/AST/DeclCXX.h" 00017 #include "clang/AST/DeclTemplate.h" 00018 #include "clang/AST/Expr.h" 00019 #include "clang/Basic/IdentifierTable.h" 00020 #include "clang/Sema/SemaInternal.h" 00021 #include "llvm/ADT/SmallString.h" 00022 #include "llvm/ADT/StringSwitch.h" 00023 using namespace clang; 00024 00025 IdentifierLoc *IdentifierLoc::create(ASTContext &Ctx, SourceLocation Loc, 00026 IdentifierInfo *Ident) { 00027 IdentifierLoc *Result = new (Ctx) IdentifierLoc; 00028 Result->Loc = Loc; 00029 Result->Ident = Ident; 00030 return Result; 00031 } 00032 00033 size_t AttributeList::allocated_size() const { 00034 if (IsAvailability) return AttributeFactory::AvailabilityAllocSize; 00035 else if (IsTypeTagForDatatype) 00036 return AttributeFactory::TypeTagForDatatypeAllocSize; 00037 else if (IsProperty) 00038 return AttributeFactory::PropertyAllocSize; 00039 return (sizeof(AttributeList) + NumArgs * sizeof(ArgsUnion)); 00040 } 00041 00042 AttributeFactory::AttributeFactory() { 00043 // Go ahead and configure all the inline capacity. This is just a memset. 00044 FreeLists.resize(InlineFreeListsCapacity); 00045 } 00046 AttributeFactory::~AttributeFactory() {} 00047 00048 static size_t getFreeListIndexForSize(size_t size) { 00049 assert(size >= sizeof(AttributeList)); 00050 assert((size % sizeof(void*)) == 0); 00051 return ((size - sizeof(AttributeList)) / sizeof(void*)); 00052 } 00053 00054 void *AttributeFactory::allocate(size_t size) { 00055 // Check for a previously reclaimed attribute. 00056 size_t index = getFreeListIndexForSize(size); 00057 if (index < FreeLists.size()) { 00058 if (AttributeList *attr = FreeLists[index]) { 00059 FreeLists[index] = attr->NextInPool; 00060 return attr; 00061 } 00062 } 00063 00064 // Otherwise, allocate something new. 00065 return Alloc.Allocate(size, llvm::AlignOf<AttributeFactory>::Alignment); 00066 } 00067 00068 void AttributeFactory::reclaimPool(AttributeList *cur) { 00069 assert(cur && "reclaiming empty pool!"); 00070 do { 00071 // Read this here, because we're going to overwrite NextInPool 00072 // when we toss 'cur' into the appropriate queue. 00073 AttributeList *next = cur->NextInPool; 00074 00075 size_t size = cur->allocated_size(); 00076 size_t freeListIndex = getFreeListIndexForSize(size); 00077 00078 // Expand FreeLists to the appropriate size, if required. 00079 if (freeListIndex >= FreeLists.size()) 00080 FreeLists.resize(freeListIndex+1); 00081 00082 // Add 'cur' to the appropriate free-list. 00083 cur->NextInPool = FreeLists[freeListIndex]; 00084 FreeLists[freeListIndex] = cur; 00085 00086 cur = next; 00087 } while (cur); 00088 } 00089 00090 void AttributePool::takePool(AttributeList *pool) { 00091 assert(pool); 00092 00093 // Fast path: this pool is empty. 00094 if (!Head) { 00095 Head = pool; 00096 return; 00097 } 00098 00099 // Reverse the pool onto the current head. This optimizes for the 00100 // pattern of pulling a lot of pools into a single pool. 00101 do { 00102 AttributeList *next = pool->NextInPool; 00103 pool->NextInPool = Head; 00104 Head = pool; 00105 pool = next; 00106 } while (pool); 00107 } 00108 00109 #include "clang/Sema/AttrParsedAttrKinds.inc" 00110 00111 AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name, 00112 const IdentifierInfo *ScopeName, 00113 Syntax SyntaxUsed) { 00114 StringRef AttrName = Name->getName(); 00115 00116 SmallString<64> FullName; 00117 if (ScopeName) 00118 FullName += ScopeName->getName(); 00119 00120 // Normalize the attribute name, __foo__ becomes foo. This is only allowable 00121 // for GNU attributes. 00122 bool IsGNU = SyntaxUsed == AS_GNU || (SyntaxUsed == AS_CXX11 && 00123 FullName == "gnu"); 00124 if (IsGNU && AttrName.size() >= 4 && AttrName.startswith("__") && 00125 AttrName.endswith("__")) 00126 AttrName = AttrName.slice(2, AttrName.size() - 2); 00127 00128 // Ensure that in the case of C++11 attributes, we look for '::foo' if it is 00129 // unscoped. 00130 if (ScopeName || SyntaxUsed == AS_CXX11) 00131 FullName += "::"; 00132 FullName += AttrName; 00133 00134 return ::getAttrKind(FullName, SyntaxUsed); 00135 } 00136 00137 unsigned AttributeList::getAttributeSpellingListIndex() const { 00138 // Both variables will be used in tablegen generated 00139 // attribute spell list index matching code. 00140 StringRef Name = AttrName->getName(); 00141 StringRef Scope = ScopeName ? ScopeName->getName() : ""; 00142 00143 #include "clang/Sema/AttrSpellingListIndex.inc" 00144 00145 } 00146 00147 struct ParsedAttrInfo { 00148 unsigned NumArgs : 4; 00149 unsigned OptArgs : 4; 00150 unsigned HasCustomParsing : 1; 00151 unsigned IsTargetSpecific : 1; 00152 unsigned IsType : 1; 00153 unsigned IsKnownToGCC : 1; 00154 00155 bool (*DiagAppertainsToDecl)(Sema &S, const AttributeList &Attr, 00156 const Decl *); 00157 bool (*DiagLangOpts)(Sema &S, const AttributeList &Attr); 00158 bool (*ExistsInTarget)(const llvm::Triple &T); 00159 unsigned (*SpellingIndexToSemanticSpelling)(const AttributeList &Attr); 00160 }; 00161 00162 namespace { 00163 #include "clang/Sema/AttrParsedAttrImpl.inc" 00164 } 00165 00166 static const ParsedAttrInfo &getInfo(const AttributeList &A) { 00167 return AttrInfoMap[A.getKind()]; 00168 } 00169 00170 unsigned AttributeList::getMinArgs() const { 00171 return getInfo(*this).NumArgs; 00172 } 00173 00174 unsigned AttributeList::getMaxArgs() const { 00175 return getMinArgs() + getInfo(*this).OptArgs; 00176 } 00177 00178 bool AttributeList::hasCustomParsing() const { 00179 return getInfo(*this).HasCustomParsing; 00180 } 00181 00182 bool AttributeList::diagnoseAppertainsTo(Sema &S, const Decl *D) const { 00183 return getInfo(*this).DiagAppertainsToDecl(S, *this, D); 00184 } 00185 00186 bool AttributeList::diagnoseLangOpts(Sema &S) const { 00187 return getInfo(*this).DiagLangOpts(S, *this); 00188 } 00189 00190 bool AttributeList::isTargetSpecificAttr() const { 00191 return getInfo(*this).IsTargetSpecific; 00192 } 00193 00194 bool AttributeList::isTypeAttr() const { 00195 return getInfo(*this).IsType; 00196 } 00197 00198 bool AttributeList::existsInTarget(const llvm::Triple &T) const { 00199 return getInfo(*this).ExistsInTarget(T); 00200 } 00201 00202 bool AttributeList::isKnownToGCC() const { 00203 return getInfo(*this).IsKnownToGCC; 00204 } 00205 00206 unsigned AttributeList::getSemanticSpelling() const { 00207 return getInfo(*this).SpellingIndexToSemanticSpelling(*this); 00208 } 00209 00210 bool AttributeList::hasVariadicArg() const { 00211 // If the attribute has the maximum number of optional arguments, we will 00212 // claim that as being variadic. If we someday get an attribute that 00213 // legitimately bumps up against that maximum, we can use another bit to track 00214 // whether it's truly variadic or not. 00215 return getInfo(*this).OptArgs == 15; 00216 }