2007-06-09 11:39:29 +08:00
|
|
|
//===--- AttributeList.cpp --------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-06-09 11:39:29 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the AttributeList class implementation
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-08-21 02:27:03 +08:00
|
|
|
#include "clang/Sema/AttributeList.h"
|
2012-07-05 01:04:04 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2013-11-27 21:27:02 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2014-01-07 19:51:46 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
2009-04-12 02:48:18 +08:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
2014-01-07 19:51:46 +08:00
|
|
|
#include "clang/Sema/SemaInternal.h"
|
2012-05-04 02:27:39 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2007-06-09 11:39:29 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2013-09-04 02:01:40 +08:00
|
|
|
IdentifierLoc *IdentifierLoc::create(ASTContext &Ctx, SourceLocation Loc,
|
|
|
|
IdentifierInfo *Ident) {
|
|
|
|
IdentifierLoc *Result = new (Ctx) IdentifierLoc;
|
|
|
|
Result->Loc = Loc;
|
|
|
|
Result->Ident = Ident;
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2011-03-24 19:26:52 +08:00
|
|
|
size_t AttributeList::allocated_size() const {
|
|
|
|
if (IsAvailability) return AttributeFactory::AvailabilityAllocSize;
|
2012-08-17 08:08:38 +08:00
|
|
|
else if (IsTypeTagForDatatype)
|
|
|
|
return AttributeFactory::TypeTagForDatatypeAllocSize;
|
2013-04-16 15:28:30 +08:00
|
|
|
else if (IsProperty)
|
|
|
|
return AttributeFactory::PropertyAllocSize;
|
2013-08-31 09:11:41 +08:00
|
|
|
return (sizeof(AttributeList) + NumArgs * sizeof(ArgsUnion));
|
2011-03-24 19:26:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AttributeFactory::AttributeFactory() {
|
|
|
|
// Go ahead and configure all the inline capacity. This is just a memset.
|
|
|
|
FreeLists.resize(InlineFreeListsCapacity);
|
|
|
|
}
|
|
|
|
AttributeFactory::~AttributeFactory() {}
|
|
|
|
|
|
|
|
static size_t getFreeListIndexForSize(size_t size) {
|
|
|
|
assert(size >= sizeof(AttributeList));
|
|
|
|
assert((size % sizeof(void*)) == 0);
|
|
|
|
return ((size - sizeof(AttributeList)) / sizeof(void*));
|
|
|
|
}
|
|
|
|
|
|
|
|
void *AttributeFactory::allocate(size_t size) {
|
|
|
|
// Check for a previously reclaimed attribute.
|
|
|
|
size_t index = getFreeListIndexForSize(size);
|
|
|
|
if (index < FreeLists.size()) {
|
|
|
|
if (AttributeList *attr = FreeLists[index]) {
|
|
|
|
FreeLists[index] = attr->NextInPool;
|
|
|
|
return attr;
|
|
|
|
}
|
2009-02-19 14:25:12 +08:00
|
|
|
}
|
2011-03-24 19:26:52 +08:00
|
|
|
|
|
|
|
// Otherwise, allocate something new.
|
|
|
|
return Alloc.Allocate(size, llvm::AlignOf<AttributeFactory>::Alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AttributeFactory::reclaimPool(AttributeList *cur) {
|
|
|
|
assert(cur && "reclaiming empty pool!");
|
|
|
|
do {
|
|
|
|
// Read this here, because we're going to overwrite NextInPool
|
|
|
|
// when we toss 'cur' into the appropriate queue.
|
|
|
|
AttributeList *next = cur->NextInPool;
|
|
|
|
|
|
|
|
size_t size = cur->allocated_size();
|
|
|
|
size_t freeListIndex = getFreeListIndexForSize(size);
|
|
|
|
|
|
|
|
// Expand FreeLists to the appropriate size, if required.
|
|
|
|
if (freeListIndex >= FreeLists.size())
|
|
|
|
FreeLists.resize(freeListIndex+1);
|
|
|
|
|
|
|
|
// Add 'cur' to the appropriate free-list.
|
|
|
|
cur->NextInPool = FreeLists[freeListIndex];
|
|
|
|
FreeLists[freeListIndex] = cur;
|
|
|
|
|
|
|
|
cur = next;
|
|
|
|
} while (cur);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AttributePool::takePool(AttributeList *pool) {
|
|
|
|
assert(pool);
|
|
|
|
|
|
|
|
// Fast path: this pool is empty.
|
|
|
|
if (!Head) {
|
|
|
|
Head = pool;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reverse the pool onto the current head. This optimizes for the
|
|
|
|
// pattern of pulling a lot of pools into a single pool.
|
|
|
|
do {
|
|
|
|
AttributeList *next = pool->NextInPool;
|
|
|
|
pool->NextInPool = Head;
|
|
|
|
Head = pool;
|
|
|
|
pool = next;
|
|
|
|
} while (pool);
|
2007-06-09 11:39:29 +08:00
|
|
|
}
|
2008-02-21 07:14:47 +08:00
|
|
|
|
2012-05-03 01:33:51 +08:00
|
|
|
#include "clang/Sema/AttrParsedAttrKinds.inc"
|
|
|
|
|
2012-05-04 02:27:39 +08:00
|
|
|
AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name,
|
2012-06-19 00:13:52 +08:00
|
|
|
const IdentifierInfo *ScopeName,
|
|
|
|
Syntax SyntaxUsed) {
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef AttrName = Name->getName();
|
2008-02-21 07:14:47 +08:00
|
|
|
|
2013-12-12 06:27:44 +08:00
|
|
|
SmallString<64> FullName;
|
|
|
|
if (ScopeName)
|
|
|
|
FullName += ScopeName->getName();
|
|
|
|
|
|
|
|
// Normalize the attribute name, __foo__ becomes foo. This is only allowable
|
|
|
|
// for GNU attributes.
|
|
|
|
bool IsGNU = SyntaxUsed == AS_GNU || (SyntaxUsed == AS_CXX11 &&
|
2014-03-09 13:18:27 +08:00
|
|
|
FullName == "gnu");
|
2013-12-12 06:27:44 +08:00
|
|
|
if (IsGNU && AttrName.size() >= 4 && AttrName.startswith("__") &&
|
2013-12-06 02:04:42 +08:00
|
|
|
AttrName.endswith("__"))
|
2013-12-12 06:27:44 +08:00
|
|
|
AttrName = AttrName.slice(2, AttrName.size() - 2);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-06-19 00:13:52 +08:00
|
|
|
// Ensure that in the case of C++11 attributes, we look for '::foo' if it is
|
|
|
|
// unscoped.
|
|
|
|
if (ScopeName || SyntaxUsed == AS_CXX11)
|
2013-12-12 06:27:44 +08:00
|
|
|
FullName += "::";
|
|
|
|
FullName += AttrName;
|
2012-06-19 00:13:52 +08:00
|
|
|
|
2014-01-14 05:42:39 +08:00
|
|
|
return ::getAttrKind(FullName, SyntaxUsed);
|
2008-02-21 07:14:47 +08:00
|
|
|
}
|
2013-01-25 00:46:58 +08:00
|
|
|
|
|
|
|
unsigned AttributeList::getAttributeSpellingListIndex() const {
|
|
|
|
// Both variables will be used in tablegen generated
|
|
|
|
// attribute spell list index matching code.
|
|
|
|
StringRef Name = AttrName->getName();
|
|
|
|
StringRef Scope = ScopeName ? ScopeName->getName() : "";
|
|
|
|
|
|
|
|
#include "clang/Sema/AttrSpellingListIndex.inc"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-09-10 07:33:17 +08:00
|
|
|
struct ParsedAttrInfo {
|
|
|
|
unsigned NumArgs : 4;
|
|
|
|
unsigned OptArgs : 4;
|
|
|
|
unsigned HasCustomParsing : 1;
|
2014-01-10 06:48:32 +08:00
|
|
|
unsigned IsTargetSpecific : 1;
|
|
|
|
unsigned IsType : 1;
|
2014-01-28 06:10:04 +08:00
|
|
|
unsigned IsKnownToGCC : 1;
|
2013-11-27 21:27:02 +08:00
|
|
|
|
|
|
|
bool (*DiagAppertainsToDecl)(Sema &S, const AttributeList &Attr,
|
|
|
|
const Decl *);
|
2013-12-03 03:30:36 +08:00
|
|
|
bool (*DiagLangOpts)(Sema &S, const AttributeList &Attr);
|
2014-03-05 03:31:42 +08:00
|
|
|
bool (*ExistsInTarget)(const llvm::Triple &T);
|
2014-01-25 05:32:49 +08:00
|
|
|
unsigned (*SpellingIndexToSemanticSpelling)(const AttributeList &Attr);
|
2013-09-10 07:33:17 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
#include "clang/Sema/AttrParsedAttrImpl.inc"
|
|
|
|
}
|
|
|
|
|
2013-09-28 23:08:41 +08:00
|
|
|
static const ParsedAttrInfo &getInfo(const AttributeList &A) {
|
2013-09-10 07:33:17 +08:00
|
|
|
return AttrInfoMap[A.getKind()];
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned AttributeList::getMinArgs() const {
|
|
|
|
return getInfo(*this).NumArgs;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned AttributeList::getMaxArgs() const {
|
|
|
|
return getMinArgs() + getInfo(*this).OptArgs;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AttributeList::hasCustomParsing() const {
|
|
|
|
return getInfo(*this).HasCustomParsing;
|
|
|
|
}
|
2013-11-27 21:27:02 +08:00
|
|
|
|
|
|
|
bool AttributeList::diagnoseAppertainsTo(Sema &S, const Decl *D) const {
|
|
|
|
return getInfo(*this).DiagAppertainsToDecl(S, *this, D);
|
|
|
|
}
|
2013-12-03 03:30:36 +08:00
|
|
|
|
|
|
|
bool AttributeList::diagnoseLangOpts(Sema &S) const {
|
|
|
|
return getInfo(*this).DiagLangOpts(S, *this);
|
|
|
|
}
|
2014-01-10 06:48:32 +08:00
|
|
|
|
|
|
|
bool AttributeList::isTargetSpecificAttr() const {
|
|
|
|
return getInfo(*this).IsTargetSpecific;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AttributeList::isTypeAttr() const {
|
|
|
|
return getInfo(*this).IsType;
|
|
|
|
}
|
|
|
|
|
2014-03-05 03:31:42 +08:00
|
|
|
bool AttributeList::existsInTarget(const llvm::Triple &T) const {
|
2014-01-10 06:48:32 +08:00
|
|
|
return getInfo(*this).ExistsInTarget(T);
|
|
|
|
}
|
2014-01-21 01:18:35 +08:00
|
|
|
|
2014-01-28 06:10:04 +08:00
|
|
|
bool AttributeList::isKnownToGCC() const {
|
|
|
|
return getInfo(*this).IsKnownToGCC;
|
2014-01-21 01:18:35 +08:00
|
|
|
}
|
2014-01-25 05:32:49 +08:00
|
|
|
|
|
|
|
unsigned AttributeList::getSemanticSpelling() const {
|
|
|
|
return getInfo(*this).SpellingIndexToSemanticSpelling(*this);
|
|
|
|
}
|