2018-02-20 10:16:28 +08:00
|
|
|
//===- AttributeList.cpp --------------------------------------------------===//
|
2007-06-09 11:39:29 +08:00
|
|
|
//
|
|
|
|
// 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"
|
2017-04-18 22:33:39 +08:00
|
|
|
#include "clang/Basic/AttrSubjectMatchRules.h"
|
2009-04-12 02:48:18 +08:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
2015-07-21 06:57:31 +08:00
|
|
|
#include "clang/Basic/TargetInfo.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"
|
2018-02-20 10:16:28 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <utility>
|
|
|
|
|
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);
|
|
|
|
}
|
2018-02-20 10:16:28 +08:00
|
|
|
AttributeFactory::~AttributeFactory() = default;
|
2011-03-24 19:26:52 +08:00
|
|
|
|
|
|
|
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.
|
2016-10-20 22:27:22 +08:00
|
|
|
return Alloc.Allocate(size, alignof(AttributeFactory));
|
2011-03-24 19:26:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
2015-08-26 00:44:38 +08:00
|
|
|
static StringRef normalizeAttrName(StringRef AttrName, StringRef ScopeName,
|
|
|
|
AttributeList::Syntax SyntaxUsed) {
|
|
|
|
// Normalize the attribute name, __foo__ becomes foo. This is only allowable
|
|
|
|
// for GNU attributes.
|
|
|
|
bool IsGNU = SyntaxUsed == AttributeList::AS_GNU ||
|
2017-10-15 23:01:42 +08:00
|
|
|
((SyntaxUsed == AttributeList::AS_CXX11 ||
|
|
|
|
SyntaxUsed == AttributeList::AS_C2x) && ScopeName == "gnu");
|
2015-08-26 00:44:38 +08:00
|
|
|
if (IsGNU && AttrName.size() >= 4 && AttrName.startswith("__") &&
|
|
|
|
AttrName.endswith("__"))
|
|
|
|
AttrName = AttrName.slice(2, AttrName.size() - 2);
|
|
|
|
|
|
|
|
return AttrName;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2015-08-26 00:44:38 +08:00
|
|
|
AttrName = normalizeAttrName(AttrName, FullName, SyntaxUsed);
|
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.
|
2017-10-15 23:01:42 +08:00
|
|
|
if (ScopeName || SyntaxUsed == AS_CXX11 || SyntaxUsed == AS_C2x)
|
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 Scope = ScopeName ? ScopeName->getName() : "";
|
2015-08-26 00:44:38 +08:00
|
|
|
StringRef Name = normalizeAttrName(AttrName->getName(), Scope,
|
|
|
|
(AttributeList::Syntax)SyntaxUsed);
|
2013-01-25 00:46:58 +08:00
|
|
|
|
|
|
|
#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;
|
2016-03-08 08:32:55 +08:00
|
|
|
unsigned IsStmt : 1;
|
2014-01-28 06:10:04 +08:00
|
|
|
unsigned IsKnownToGCC : 1;
|
2017-04-18 22:33:39 +08:00
|
|
|
unsigned IsSupportedByPragmaAttribute : 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);
|
2015-07-21 06:57:31 +08:00
|
|
|
bool (*ExistsInTarget)(const TargetInfo &Target);
|
2014-01-25 05:32:49 +08:00
|
|
|
unsigned (*SpellingIndexToSemanticSpelling)(const AttributeList &Attr);
|
2017-04-18 22:33:39 +08:00
|
|
|
void (*GetPragmaAttributeMatchRules)(
|
|
|
|
llvm::SmallVectorImpl<std::pair<attr::SubjectMatchRule, bool>> &Rules,
|
|
|
|
const LangOptions &LangOpts);
|
2013-09-10 07:33:17 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
2018-02-20 10:16:28 +08:00
|
|
|
|
|
|
|
#include "clang/Sema/AttrParsedAttrImpl.inc"
|
|
|
|
|
|
|
|
} // namespace
|
2013-09-10 07:33:17 +08:00
|
|
|
|
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
|
|
|
|
2017-04-18 22:33:39 +08:00
|
|
|
bool AttributeList::appliesToDecl(const Decl *D,
|
|
|
|
attr::SubjectMatchRule MatchRule) const {
|
|
|
|
return checkAttributeMatchRuleAppliesTo(D, MatchRule);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AttributeList::getMatchRules(
|
|
|
|
const LangOptions &LangOpts,
|
|
|
|
SmallVectorImpl<std::pair<attr::SubjectMatchRule, bool>> &MatchRules)
|
|
|
|
const {
|
|
|
|
return getInfo(*this).GetPragmaAttributeMatchRules(MatchRules, LangOpts);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-03-08 08:32:55 +08:00
|
|
|
bool AttributeList::isStmtAttr() const {
|
|
|
|
return getInfo(*this).IsStmt;
|
|
|
|
}
|
|
|
|
|
2015-07-21 06:57:31 +08:00
|
|
|
bool AttributeList::existsInTarget(const TargetInfo &Target) const {
|
|
|
|
return getInfo(*this).ExistsInTarget(Target);
|
2014-01-10 06:48:32 +08:00
|
|
|
}
|
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
|
|
|
|
2017-04-18 22:33:39 +08:00
|
|
|
bool AttributeList::isSupportedByPragmaAttribute() const {
|
|
|
|
return getInfo(*this).IsSupportedByPragmaAttribute;
|
|
|
|
}
|
|
|
|
|
2014-01-25 05:32:49 +08:00
|
|
|
unsigned AttributeList::getSemanticSpelling() const {
|
|
|
|
return getInfo(*this).SpellingIndexToSemanticSpelling(*this);
|
|
|
|
}
|
2014-08-01 00:37:04 +08:00
|
|
|
|
|
|
|
bool AttributeList::hasVariadicArg() const {
|
|
|
|
// If the attribute has the maximum number of optional arguments, we will
|
|
|
|
// claim that as being variadic. If we someday get an attribute that
|
|
|
|
// legitimately bumps up against that maximum, we can use another bit to track
|
|
|
|
// whether it's truly variadic or not.
|
|
|
|
return getInfo(*this).OptArgs == 15;
|
|
|
|
}
|